Spring框架[email protected]

使用@Resource注解,实现组件装配,默认按照名称匹配。

下面的类都是使用到注解的类:

 1 package dao.impl;
 2
 3 import org.springframework.stereotype.Repository;
 4
 5 import dao.UserDao;
 6 import entity.User;
 7
 8 /**
 9  * 用户DAO类,实现IDao接口,负责User类的持久化操作
10  */
11 @Repository("userDao")
12 public class UserDaoImpl implements UserDao {
13
14     public void save(User user) {
15         // 这里并未实现完整的数据库操作,仅为说明问题
16         System.out.println("保存用户信息到数据库");
17         //throw new RuntimeException("为了测试程序异常");
18     }
19 }
 1     package service.impl;
 2
 3     import javax.annotation.Resource;
 4
 5     import org.springframework.stereotype.Service;
 6
 7     import service.UserService;
 8     import dao.UserDao;
 9     import entity.User;
10
11     /**
12      * 用户业务类,实现对User功能的业务管理
13      */
14     @Service("userService")
15     public class UserServiceImpl implements UserService {
16
17         // 声明接口类型的引用,和具体实现类解耦合
18         @Resource(name="userDao")//通过resource注解进行装配
19         private UserDao dao;
20
21
22         public UserDao getDao() {
23             return dao;
24         }
25
26
27         public void setDao(UserDao dao) {
28             this.dao = dao;
29         }
30
31
32         public void addNewUser(User user) {
33             // 调用用户DAO的方法保存用户信息
34             dao.save(user);
35             System.out.println("注入进去的user对象的信息是:"+user.toString());
36         }
37     }

applicationContext.xml配置文件

<?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-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  <!-- 扫描包中注解标注的类 -->
  <context:component-scan base-package="service,dao" />
</beans>

测试方法:

 1 package test;
 2
 3 import org.junit.Test;
 4 import org.springframework.context.ApplicationContext;
 5 import org.springframework.context.support.ClassPathXmlApplicationContext;
 6
 7 import service.UserService;
 8 import service.impl.UserServiceImpl;
 9
10 import entity.TestEntity;
11 import entity.User;
12
13
14 public class AopTest {
15
16 17
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService a = (UserService) ctx.getBean("userService");
        User user=new User();
        user.setUsername("丫丫");
        a.addNewUser(user);
24 25 }

运行结果:

保存用户信息到数据库
注入进去的user对象的信息是:[email protected]

现在,将@Resource(name="userDao")的name属性去掉,@Resource

运行结果:

保存用户信息到数据库
注入进去的user对象的信息是:[email protected]

原因:resource属性默认是按照属性名称进行装配的,如果属性名称不对应的话,他会按照类型进行匹配的。

同样,这个属性也能够添加到setter方法上面

运行结果:

保存用户信息到数据库
注入进去的user对象的信息是:[email protected]

Resource注解默认的按照名称进行寻找指的是:public void setUserDao(UserDao dao) {   按照set后面的名称去寻找的

相应的我修改了:

 1 package dao.impl;
 2
 3 import org.springframework.stereotype.Repository;
 4
 5 import dao.UserDao;
 6 import entity.User;
 7
 8 /**
 9  * 用户DAO类,实现IDao接口,负责User类的持久化操作
10  */
11 @Repository("dao")
12 public class UserDaoImpl implements UserDao {
13
14     public void save(User user) {
15         // 这里并未实现完整的数据库操作,仅为说明问题
16         System.out.println("保存用户信息到数据库");
17         //throw new RuntimeException("为了测试程序异常");
18     }
19 }
 1 package dao.impl;
 2
 3 import org.springframework.stereotype.Repository;
 4
 5 import dao.UserDao;
 6 import entity.User;
 7
 8 /**
 9  * 用户DAO类,实现IDao接口,负责User类的持久化操作
10  */
11 @Repository("userDao")
12 public class UserDaoImpl2 implements UserDao {
13
14     public void save(User user) {
15         // 这里并未实现完整的数据库操作,仅为说明问题
16         System.out.println("保存用户信息到数据库111111111111");
17         //throw new RuntimeException("为了测试程序异常");
18     }
19 }
 1     package service.impl;
 2
 3     import javax.annotation.Resource;
 4
 5     import org.springframework.stereotype.Service;
 6
 7     import service.UserService;
 8     import dao.UserDao;
 9     import entity.User;
10
11     /**
12      * 用户业务类,实现对User功能的业务管理
13      */
14     @Service("userService")
15     public class UserServiceImpl implements UserService {
16
17         // 声明接口类型的引用,和具体实现类解耦合
18
19         private UserDao dao;
20
21
22         public UserDao getDao() {
23             return dao;
24         }
25
26         @Resource//通过resource注解进行装配
27         public void setUserDao(UserDao dao) {
28             this.dao = dao;
29         }
30
31
32
33         public void addNewUser(User user) {
34             // 调用用户DAO的方法保存用户信息
35             dao.save(user);
36             System.out.println("注入进去的user对象的信息是:"+user.toString());
37         }
38     }

运行测试方法:

package test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import service.UserService;
import service.impl.UserServiceImpl;

import entity.TestEntity;
import entity.User;

public class AopTest {

    @Test
    public void aopTest() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService a = (UserService) ctx.getBean("userService");
        User user=new User();
        user.setUsername("丫丫");
        a.addNewUser(user);
    }

}

运行结果:

保存用户信息到数据库111111111111
注入进去的user对象的信息是:[email protected]

原文地址:https://www.cnblogs.com/dongyaotou/p/12122796.html

时间: 2024-07-28 14:19:28

Spring框架[email protected]的相关文章

pytest框架[email&#160;protected](scpoe=‘xxx&#39;)装饰器

pytest是一个非常成熟的全功能的Python测试框架 用例设计原则 谨记:当我们使用pytest框架写case的时候,一定要拿它的命令规范去case,这样框架才能识别到哪些case需要执行,哪些不需要执行文件名以test_.py文件和_test.py以Test开头的类以test_开头的函数以test_开头的方法 运行方式可以通过pycharm里的终端命令,也可以pytest.main()运行(主推) @pytest.fixture() 即测试用例执行的环境准备和清理,相当于unittest的

Spring In [email&#160;protected]注解

//@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置,下面会将)@Component package com.zte.springinaction.soundsystem.imp; import org.springframework.stereotype.Component; import com.zte.springinaction.soundsystem.CompactDisc; //@Component注解会告诉

Spring In [email&#160;protected]注解给Bean命名

package soundsystem; import org.springframework.stereotype.Component; //@Component注解会告诉Spring创建这个类的实例bean(注意,启动Component注解功能需要在xml里面配置) @Component("newName") public class SgtPeppers implements CompactDisc { private String title="Pepper's Lo

[转]Spring注解[email&#160;protected]注解、@Bean注解以及配置自动扫描、bean作用域

1.@Configuration标注在类上,相当于把该类作为spring的xml配置文件中的<beans>,作用为:配置spring容器(应用上下文) package com.test.spring.support.configuration; @Configuration public class TestConfiguration { public TestConfiguration(){ System.out.println("spring容器启动初始化...");

Spring In [email&#160;protected]条件化Bean

@Conditional根据某个条件来决定是否创建Bean实例 代码下载地址:http://download.csdn.net/download/poiuy1991719/9965794 创建一个Bean类: package com.bean.conditional; import org.springframework.context.annotation.Conditional; import org.springframework.stereotype.Component; @Compon

Spring AOP @[email&#160;protected]@[email&#160;protected]@afterthrowing执行顺序

public Object aop(Method method,Object object) { try { try { /*doAround start*/ doBefore(); method.invoke(object); /*doAround end*/ } finally { doAfter(); } doAfterReturning(); } catch (Exception e) { doAfterThrowing(); } } 原文地址:https://www.cnblogs.c

Spring4 In [email&#160;protected]单例、多例Bean

Spring In [email protected]单例.多例Bean 代码下载地址:http://download.csdn.net/download/poiuy1991719/9967494 Singleton:单例    @Scope(ConfigurableBeanFactory.SCOPE_SINGLETON)@Scope("singleton") Prototype:每次注入都会创建新的   @Scope(ConfigurableBeanFactory.SCOPE_PRO

Spring高级话题[email&#160;protected]***注解的工作原理

出自:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy注解 激活Aspect自动代理 <aop:aspectj-autoproxy/> 开启对AspectJ自动代理的支持. 在用到AOP的自动代理的时候用,如果你理解了Java的动态代理,很容易的就会熟悉AOP的自动代理的. @EnableAsync @EnableAsync注解开启异步方法的支持. 这个相信大家都比较熟悉的.对于异步

[email&#160;protected] 深入学习之——初探spring mvc

一.简介 Spring MVC是Spring框架的最重要的模块之一,它构建于Spring IoC容器之上,大量使用容器的特性简化其配置.MVC模式消除了业务逻辑与UI的耦合.模式负责封装视图展示的应用数据:视图只显示数据,不包含任何业务逻辑:控制器负责接收用户请求并调用后端服务进行业务处理,处理之后,后端服务可能返回某些数据供视图显示.其核心思想是分离业务逻辑与UI,使系统能够独立修改,互不影响. Spring MVC应用,模式通常由服务层处理和持续层存储的领域对象组成.视图通常是用Java标准