一.IOC(inverse of control)
两种方式
1.XML配置:set方法
引入相关jar包
commons-logging-1.0.4.jar
spring-aop-4.0.1.RELEASE.jar
spring-beans-4.0.1.RELEASE.jar
spring-context-4.0.1.RELEASE.jar
spring-core-4.0.1.RELEASE.jar
spring-expression-4.0.1.RELEASE.jar
<bean id="a" class="com.communityhealth.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.communityhealth.service.impl.UserServiceImpl">
<property name="userdao" ref="a"></property>
</bean>
2.注解的方式annotation:目的---简化beans.xml配置
步骤:1.引入相关jar包
commons-logging-1.0.4.jar
spring-aop-4.0.1.RELEASE.jar
spring-beans-4.0.1.RELEASE.jar
spring-context-4.0.1.RELEASE.jar
spring-core-4.0.1.RELEASE.jar
spring-expression-4.0.1.RELEASE.jar
2.beans.xml文件中添加xmlns:context。。。以及xsi:schemaLocation相关内容
3.<context:component-scan base-package="即将被扫描的包名称(子包也会被扫描)"/>
4.@Component放在即将被实例化的类上
@Resource放在即将被注入的属性上
注意点:①:@Resource注解在哪个set方法上,就只注入拿到的对象到该方法上;
注解到属性上,只对该属性的set方法起作用
②:@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。
转载请注明原文地址: https://ju.6miu.com/read-15714.html