使用工厂方法实例化bean
<bean id="personService2" class="com.maple.service.impl.PersonServiceBeanFactory"
factory-method="createPersonServiceBean"></bean>
<bean id="personServiceFactory" class="com.maple.service.impl.PersonServiceBeanFactory"></bean>
<bean id="personService3" factory-bean="personServiceFactory"
factory-method="createPersonServiceBean2"></bean>
一,手工装配
1,基于xml配置
<bean id="personService" class="com.maple.service.impl.PersonServiceBean"> <property name="personDao"> <bean class="com.maple.dao.impl.PersonDaoBean"></bean> </property> <property name="name" value="maple"></property> //属性setter方法注入 <property name="id" value="15"></property> </bean> <bean id="personService" class="com.maple.service.impl.PersonServiceBean"> //构造器注入 <constructor-arg index="0" type="com.maple.dao.PersonDao" ref="personDao"></constructor-arg> <constructor-arg index="1" value="dancing"></constructor-arg> </bean>
2,在java代码中使用@Autowired或@Resource
但在xml文件中需要配置
<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-2.5.xsd http://www.springframework.org/schema/context !!!!! http://www.springframework.org/schema/context/spring-context-2.5.xsd"> !!!! <context:annotation-config></context:annotation-config> //注册一个处理器 </beans>
@Autowired默认按类型查找,@Qualifier(" XXX")可以使其改变为按名查找;
@Resource默认按名查找,找不到才按类型查找,如果设置了name属性,而又没有匹配到,则出错
二,自动装配
<bean id="" class="" autowire="byType/byName/constructor/autodetect"> </bean>
byType:按类型,查找到多个会抛异常,没有为null,要setter方法,相当于@Autowired
byName:@Resource
constructor:也是按类型,不过是针对构造器的参数类型,即使是String类型也要在xml中配置
autodetect:通过bean类的自省机制(introspection)决定用constructor还是byType来自动装配,如发现默认构造器,使用byType
三,自动扫描机制
在类路径下寻找标注了@Component,@Service,@Controller,@Repository注解的类(类上面加注解),将这些类纳入spring容器中管理。
<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-2.5.xsd http://www.springframework.org/schema/context !!!!! http://www.springframework.org/schema/context/spring-context-2.5.xsd"> !!!!
//会注册很多处理器,包括了<context:annotation-config />的功能,还包括处理器例如用来解析@PostConstruct
<context:component-scan base-package="com.maple"/>
//base-package为需要扫描的包(含子包)
</beans>
@Component, 泛指组件,不好归类时用这个
@Service, 标注业务层组件
@Controller,标注控制层组件,如struts中的action
@Repository,标注数据访问组件,即DAO层
约定:如果PersonServiceBean类用了@Service注解,按该bean的id值默认为personServiceBean,
即使用时
@Test public void instanceSpring2() throws Exception{ AbstractApplicationContext context=new ClassPathXmlApplicationContext("beans.xml"); //MapleClassPathXMLApplicationContext context=new MapleClassPathXMLApplicationContext("beans.xml"); PersonService personService=(PersonService) context.getBean("personServiceBean"); System.out.println(personService); }
默认是单例作用域,可以使用注解@Scope("prototype")改为原型,则每次都是新的对象
@PostConstruct注解 指定类初始化的方法,替代了以前配置bean时init-method的属性,是构造方法后执行的方法
@PreDestroy, 销毁前执行的方法,替代了destroy-method