Spring(四) 自动扫描管理

来源:互联网 发布:maxwell仿真软件 编辑:程序博客网 时间:2024/06/15 14:18
<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:component-scan base-package="cn.itcast"/>

</beans>

@Service   /@Service("personService")  // Scope("prototype")
public class PersonServiceBean implements PersonService {
private PersonDao personDao;
public void setPersonDao(PersonDao personDao) {
this.personDao = personDao;

}

@PostConstruct                参见:  --------Spring(二)bean实例化-----
public void init(){
System.out.println("初始化");
}

@PreDestroy
public void destory(){
System.out.println("开闭资源");
}

}

AbstractApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
PersonService personService = (PersonService)ctx.getBean("personServiceBean");/personService
System.out.println(personService);
ctx.close();
--------------------------------------------------------------------------------------------------------------------------------------



原创粉丝点击