spring依赖注入xml与注解对比
xml方式:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="student" class="com.ren.service.impl.Student">
<property name="run" ref="runLong"></property>
<property name="name" value="renhq"></property>
</bean>
<bean id="runLong" class="com.ren.service.impl.RunLong"></bean>
<bean id="date" class="java.util.Date"></bean>
<bean id="user" class="com.ren.model.User">
<property name="name" value="renhq"></property>
<property name="address" ref="address"></property>
</bean>
<bean id="address" class="com.ren.model.Address"></bean>
</beans>
1、装配bean的方式:
UserFactory.createUser()为工厂返回一个User对象
<!--装配bean的三种方式-->
<!--1、new实现类-->
<bean id="user01" class="com.ren.model.User"></bean>
<!--2、静态工厂-->
<bean id="user02" class="com.ren.service.impl.UserFactory" factory-method="createUser"></bean>
<!--3、实例工厂-->
<!--创建工厂实例-->
<bean id="userfactory" class="com.ren.service.impl.UserFactory2"></bean>
<bean id="user03" factory-bean="userfactory" factory-method="createUser"></bean>
2、bean的作用域(单例、多例):
<!--bean的作用域-->
<!--1、singleton 单例(默认)-->
<bean id="user1" class="com.ren.model.User" scope="singleton"></bean>
<!--2、prototype-->
<bean id="user2" class="com.ren.model.User" scope="prototype"></bean>
3、bean的属性值注入:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--1.1构造方法注入属性值-->
<!--<bean id="student" class="com.ren.model.Student">-->
<!--调用 public Student(String username, String password)-->
<!--<constructor-arg name="username" value="ren"></constructor-arg>-->
<!--<constructor-arg name="password" value="123"></constructor-arg>-->
<!--</bean>-->
<!--1.2构造方法注入属性值-->
<!--<bean id="student" class="com.ren.model.Student">-->
<!--通过索引调用构造方法-->
<!--<constructor-arg index="0" value="ren" type="java.lang.String"></constructor-arg>-->
<!--<constructor-arg index="1" value="123" type="int"></constructor-arg>-->
<!--</bean>-->
<!--2、setter属性注入-->
<!--<bean id="student" class="com.ren.model.Student">-->
<!--<property name="age" value="21"></property>-->
<!--<property name="username" value="ren"></property>-->
<!--<property name="password" value="123"></property>-->
<!--</bean>-->
<!--3、通过p命名空间-->
<!--添加: xmlns:p="http://www.springframework.org/schema/p"-->
<bean id="student" class="com.ren.model.Student" p:age="21" p:password="123" p:username="ren"></bean>
</beans>
4、SpEL表达式:
<!--SpEL-->
<!--* SpEL-->
<!--* <bean id="name" class="" value="#{表达式}">-->
<!--* #{123}: 数字-->
<!--* #{'str'}: 字符串-->
<!--* #{beanId}: 其他bean-->
<!--* #{beanId.propertyName}: 其他bean属性-->
<!--* #{beanId.toString()}: 其他bean方法-->
<!--* #{T(类).方法}: 静态方法-->
<!--* #{T(类).字段}: 静态字段-->
<bean id="customer" class="com.ren.model.Customer">
<property name="name" value="#{'renhq'}"></property>
<property name="sex" value="#{'man'}"></property>
<property name="pi" value="#{T(java.lang.Math).PI}"></property>
<property name="address" value="#{address}"></property>
<!--<property name="address" ref="address"></property>-->
</bean>
<bean id="address" class="com.ren.model.Address">
<property name="name" value="#{'山东'}"></property>
</bean>
5、集合属性注入:
<!--集合属性注入-->
<!--List、Set、Map、Properties、数组-->
<bean id="programma" class="com.ren.model.Programma">
<property name="name" value="renhq"></property>
<!--List-->
<property name="cars">
<list>
<value>宝马</value>
<value>奔驰</value>
<value>劳斯莱斯</value>
</list>
</property>
<!--Set-->
<property name="pats">
<set>
<value>小猫</value>
<value>小狗</value>
</set>
</property>
<!--Map-->
<property name="infos">
<map>
<entry key="1" value="11"></entry>
<entry key="2" value="22"></entry>
</map>
</property>
<!--Properties-->
<property name="mysqlInfo">
<props>
<prop key="driver">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/databsename?useSSL=false</prop>
<prop key="user">root</prop>
<prop key="password">123456</prop>
</props>
</property>
<!--数组-->
<property name="members">
<array>
<value>哥哥</value>
<value>弟弟</value>
</array>
</property>
</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
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--开启注解-->
<context:annotation-config></context:annotation-config>
<!--注解位置 给定包名 此路径下的类使用注解-->
<context:component-scan base-package="com.ren"></context:component-scan>
</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提供)
还没有评论,来说两句吧...