标签:abstract http tap framework javaee one not 问题 body
spring没有采用约定优于配置的策略,spring要求显示指定搜索哪些路径下的Java文件。spring将会把合适的java类全部注册成spring Bean。
<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-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 自动扫描指定包及其子包下的所有Bean类 --> <context:component-scan base-package="org.crazyit.app.service"/> </beans>
<!-- 自动扫描指定包及其子包下的所有Bean类 --> <context:component-scan base-package="org.crazyit.app.service"> <!-- 只将以Chinese、Axe结尾的类当成Spring容器中的Bean --> <context:include-filter type="regex" expression=".*Chinese"/> <context:include-filter type="regex" expression=".*Axe"/> </context:component-scan>
@Controller
public class demo {
@Resource(name="user")
private User user;
@Resource(name="user")
public void setUser(User user) {
this.user = user;
}
public User getUser() {
return user;
}
}
@Controller("demo")
@Scope("prototype")
public class demo {
}
@Component
public class SteelAxe
{
public SteelAxe()
{
System.out.println("创建SteelAxe类对象实例...");
}
}
@Component
public class Chinese
{
// 执行Field注入
@Resource(name="steelAxe")
private SteelAxe steeAxe;
public Chinese() {
super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
public void init()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
public void close()
{
System.out.println("正在执行销毁之前的close方法...");
}
}
// 创建Spring容器
AbstractApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
// 注册关闭钩子
ctx.registerShutdownHook();
创建Chinese类对象实例...创建SteelAxe类对象实例...正在执行初始化的init方法...正在执行销毁之前的close方法...@Component
public class Chinese
{
// 执行Field注入
//@Resource(name="steelAxe")
//private SteelAxe steeAxe;
public Chinese() {
super();
System.out.println("创建Chinese类对象实例...");
}
@PostConstruct
public void init()
{
System.out.println("正在执行初始化的init方法...");
}
@PreDestroy
public void close()
{
System.out.println("正在执行销毁之前的close方法...");
}
}
创建Chinese类对象实例...正在执行初始化的init方法...创建SteelAxe类对象实例...正在执行销毁之前的close方法...标签:abstract http tap framework javaee one not 问题 body
原文地址:http://www.cnblogs.com/keyi/p/6797450.html