关闭

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

92人阅读 评论(0) 收藏 举报
分类:

搜索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();
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:38439次
    • 积分:1977
    • 等级:
    • 排名:第19978名
    • 原创:154篇
    • 转载:67篇
    • 译文:0篇
    • 评论:3条
    最新评论