Spring学习(三)ioc自动注入,

搜索Bean类

Spring提供如下几个Annotation来标注Spring Bean:

  • @Component: 标注一个普通的Spring Bean类
  • @Controller: 标注一个控制器组件类
  • @Service: 标注一个业务逻辑组件类
  • @Repository: 标注一个DAO组件类


xsi:schemaLocation="
       http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/jdbc
        http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
       "
       default-autowire="byName"

@Repository("UserDaoImpl")
public class UserDaoImpl implements UserDao{
    @Autowired
    private User user;
    @Override
    public void say(){
        user.say();
    }

}

public class TestSpring {
    ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

    @Test
    public void test1(){
        UserDaoImpl userDaoImpl = ctx.getBean("UserDaoImpl", UserDaoImpl.class);
        userDaoImpl.say();
    }


阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/feicongcong/article/details/53983197
个人分类: spring
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

不良信息举报

Spring学习(三)ioc自动注入,

最多只允许输入30个字

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭