spring依赖注入xml与注解对比

朱雀 2022-01-25 22:17 340阅读 0赞

xml方式:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <bean id="student" class="com.ren.service.impl.Student">
  6. <property name="run" ref="runLong"></property>
  7. <property name="name" value="renhq"></property>
  8. </bean>
  9. <bean id="runLong" class="com.ren.service.impl.RunLong"></bean>
  10. <bean id="date" class="java.util.Date"></bean>
  11. <bean id="user" class="com.ren.model.User">
  12. <property name="name" value="renhq"></property>
  13. <property name="address" ref="address"></property>
  14. </bean>
  15. <bean id="address" class="com.ren.model.Address"></bean>
  16. </beans>

1、装配bean的方式:

UserFactory.createUser()为工厂返回一个User对象

  1. <!--装配bean的三种方式-->
  2. <!--1、new实现类-->
  3. <bean id="user01" class="com.ren.model.User"></bean>
  4. <!--2、静态工厂-->
  5. <bean id="user02" class="com.ren.service.impl.UserFactory" factory-method="createUser"></bean>
  6. <!--3、实例工厂-->
  7. <!--创建工厂实例-->
  8. <bean id="userfactory" class="com.ren.service.impl.UserFactory2"></bean>
  9. <bean id="user03" factory-bean="userfactory" factory-method="createUser"></bean>

2、bean的作用域(单例、多例):

  1. <!--bean的作用域-->
  2. <!--1、singleton 单例(默认)-->
  3. <bean id="user1" class="com.ren.model.User" scope="singleton"></bean>
  4. <!--2、prototype-->
  5. <bean id="user2" class="com.ren.model.User" scope="prototype"></bean>

3、bean的属性值注入:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. <!--1.1构造方法注入属性值-->
  7. <!--<bean id="student" class="com.ren.model.Student">-->
  8. <!--调用 public Student(String username, String password)-->
  9. <!--<constructor-arg name="username" value="ren"></constructor-arg>-->
  10. <!--<constructor-arg name="password" value="123"></constructor-arg>-->
  11. <!--</bean>-->
  12. <!--1.2构造方法注入属性值-->
  13. <!--<bean id="student" class="com.ren.model.Student">-->
  14. <!--通过索引调用构造方法-->
  15. <!--<constructor-arg index="0" value="ren" type="java.lang.String"></constructor-arg>-->
  16. <!--<constructor-arg index="1" value="123" type="int"></constructor-arg>-->
  17. <!--</bean>-->
  18. <!--2、setter属性注入-->
  19. <!--<bean id="student" class="com.ren.model.Student">-->
  20. <!--<property name="age" value="21"></property>-->
  21. <!--<property name="username" value="ren"></property>-->
  22. <!--<property name="password" value="123"></property>-->
  23. <!--</bean>-->
  24. <!--3、通过p命名空间-->
  25. <!--添加: xmlns:p="http://www.springframework.org/schema/p"-->
  26. <bean id="student" class="com.ren.model.Student" p:age="21" p:password="123" p:username="ren"></bean>
  27. </beans>

4、SpEL表达式:

  1. <!--SpEL-->
  2. <!--* SpEL-->
  3. <!--* <bean id="name" class="" value="#{表达式}">-->
  4. <!--* #{123}: 数字-->
  5. <!--* #{'str'}: 字符串-->
  6. <!--* #{beanId}: 其他bean-->
  7. <!--* #{beanId.propertyName}: 其他bean属性-->
  8. <!--* #{beanId.toString()}: 其他bean方法-->
  9. <!--* #{T(类).方法}: 静态方法-->
  10. <!--* #{T(类).字段}: 静态字段-->
  11. <bean id="customer" class="com.ren.model.Customer">
  12. <property name="name" value="#{'renhq'}"></property>
  13. <property name="sex" value="#{'man'}"></property>
  14. <property name="pi" value="#{T(java.lang.Math).PI}"></property>
  15. <property name="address" value="#{address}"></property>
  16. <!--<property name="address" ref="address"></property>-->
  17. </bean>
  18. <bean id="address" class="com.ren.model.Address">
  19. <property name="name" value="#{'山东'}"></property>
  20. </bean>

5、集合属性注入:

  1. <!--集合属性注入-->
  2. <!--List、Set、Map、Properties、数组-->
  3. <bean id="programma" class="com.ren.model.Programma">
  4. <property name="name" value="renhq"></property>
  5. <!--List-->
  6. <property name="cars">
  7. <list>
  8. <value>宝马</value>
  9. <value>奔驰</value>
  10. <value>劳斯莱斯</value>
  11. </list>
  12. </property>
  13. <!--Set-->
  14. <property name="pats">
  15. <set>
  16. <value>小猫</value>
  17. <value>小狗</value>
  18. </set>
  19. </property>
  20. <!--Map-->
  21. <property name="infos">
  22. <map>
  23. <entry key="1" value="11"></entry>
  24. <entry key="2" value="22"></entry>
  25. </map>
  26. </property>
  27. <!--Properties-->
  28. <property name="mysqlInfo">
  29. <props>
  30. <prop key="driver">com.mysql.jdbc.Driver</prop>
  31. <prop key="url">jdbc:mysql://localhost:3306/databsename?useSSL=false</prop>
  32. <prop key="user">root</prop>
  33. <prop key="password">123456</prop>
  34. </props>
  35. </property>
  36. <!--数组-->
  37. <property name="members">
  38. <array>
  39. <value>哥哥</value>
  40. <value>弟弟</value>
  41. </array>
  42. </property>
  43. </bean>

注解:

xml文件开启注解(注意标签中添加的内容)

1、默认情况下注解不生效,开启注解:

xml中配置:

添加:xmlns:context=”http://www.springframework.org/schema/context“

xsi:schemaLocation=添加 http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--开启注解-->
  10. <context:annotation-config></context:annotation-config>
  11. <!--注解位置 给定包名 此路径下的类使用注解-->
  12. <context:component-scan base-package="com.ren"></context:component-scan>
  13. </beans>

2、注解使用:

bean注解:

@Component(“id”) 配置bean

web使用注解 action -> service -> dao *

@Controller : action

@Service : service

@Repository : dao

以上三种注解由Component衍生出,这三个注解代表不同的层级,加载顺序为Controller -> Service -> Repository

@Scope(“prototype”) 表示此bean开启多例模式

属性注解:

@Autowired 根据类型注入属性 * 如果是接口寻找实现类

@Qualifier(“id”) 根据id注入

@Resource(“id”) 根据id注入(查看源码,区别应该是Resource由jdk提供)

发表评论

表情:
评论列表 (有 0 条评论,340人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springxml注解对比

    前文:           Spring是一个轻量级的IoC和AOP容器框架。是为Java应用程序提供基础性服务的一套框架,目的是用于简化企业应用程序的开发,它使得开发者只需