WEB开发网
开发学院软件开发Java Spring之@Autowired问题 阅读

Spring之@Autowired问题

 2010-01-22 00:00:00 来源:WEB开发网   
核心提示: <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="

<?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-2.5.xsd 
                     http://www.springframework.org/schema/context 
                     http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 
        <!-- 引用@Autowired必须定义这个bean --> 
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
        <!-- 通过构造方法装配的Bean --> 
        <bean id="foo" class="lavasoft.springstu.anno.Foo"> 
                <constructor-arg index="0" type="java.lang.String" value="aaaa"/> 
        </bean> 
        <!-- 通过注解装配的Bean,我还以为它和Foo没关系呢 --> 
        <bean id="bar" class="lavasoft.springstu.anno.Bar"/> 
</beans>

package lavasoft.springstu.anno; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
/** 
* 测试自动装配Bean 
* 
* @author leizhimin 2009-12-23 10:55:35 
*/ 
public class Test1 { 
        public static void main(String[] args) { 
                ApplicationContext ctx = new ClassPathXmlApplicationContext("lavasoft/springstu/anno/cfg1.xml"); 
                Bar bar = (Bar) ctx.getBean("bar"); 
                bar.f1(); 
        } 
}

运行结果:

aaaa 
Process finished with exit code 0

从上面的代码中看到,Spring的注解使得配置文件的逻辑很混乱,如果项目中有大量的类似注解,那维护起来就很困难了。

建议不要使用!

出处:http://lavasoft.blog.51cto.com/62575/247831

上一页  1 2 

Tags:Spring Autowired 问题

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接