Spring的注解装配Bean
  Spring2.5 引入使用注解去定义Bean
    @Component 描述Spring框架中Bean
  Spring的框架中提供了与@Component注解等效的三个注解
    @Repository 用于对DAO实现类进行标注
    @Service 用于对Service实现类进行标注
    @Controller 用于对Controller实现类进行标注
    ***** 三个注解为了后续版本进行增强的
  使用注解,那么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.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
  <context:annotation-config/>  <!--支持注解-->
  <context:component-scan base-package="cn.yzu.spring3"/>  <!--自动扫描-->
</beans>

Bean的属性注入
  普通属性

@Value(value="你好")
private String info;

  对象属性
    @Autowired:自动装配默认使用类型注入.
    @Qualifier("userDao"):按名称进行注入.

@Autowired
@Qualifier("userDao")
private UserDao userDao;

    等价于

@Resource(name="userDao")
private UserDao userDao;

Bean其他的属性的配置
  配置Bean初始化方法和销毁方法:init-method 和 destroy-method
    @PostConstruct 初始化
    @PreDestroy 销毁
  配置Bean的作用范围:@Scope("singleton")

@Service(value="userService")
@Scope("singleton")
public class UserService {
  @Value(value="你好")
  private String info;
  @Resource(name="userDao")
  private UserDao userDao;
  public void sayHello(){
    System.out.println("Hello Spring Annotation..."+info);
  }   @PostConstruct
  public void setup(){
    System.out.println("初始化...");
  }   @PreDestroy
  public void teardown(){
    System.out.println("销毁...");
  }
}

Spring3.0提供使用Java类定义Bean信息的方法(用的很少)

@Configuration
public class BeanConfig {
  @Bean(name="car")
  public Car showCar(){
    Car car = new Car();
    car.setName("长安");
    car.setPrice(40000d);
    return car;
  }   @Bean(name="product")
  public Product initProduct(){
    Product product = new Product();
    product.setName("空调");
    product.setPrice(3000d);
    return product;
  }
}
@Test
public void demo1(){
  ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
  Car car = (Car) applicationContext.getBean("car");
  Product product = (Product) applicationContext.getBean("product");
  System.out.println(car);
  System.out.println(product);
}

实际开发中使用XML还是注解?
  XML:有利于bean管理
  注解:注入属性的时候比较方便
  两种方式结合:一般使用XML注册Bean,使用注解进行属性的注入

<?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.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
  <!--使@Resource、@ PostConstruct、@ PreDestroy、@Autowired注解生效-->
  <context:annotation-config/>   <bean id="orderDao" class="cn.yzu.spring3.demo3.OrderDao"></bean>
  <bean id="customerService" class="cn.yzu.spring3.demo3.CustomerService"></bean>
</beans>
public class CustomerService {
  @Autowired
  @Qualifier("orderDao")
  private OrderDao orderDao;   @Override
  public String toString() {
    return "CustomerService [customerDao=" + customerDao + ", orderDao="+ orderDao + "]";
  }
}

IOC装配Bean(注解方式)的更多相关文章

  1. Spring框架(3)---IOC装配Bean(注解方式)

    IOC装配Bean(注解方式) 上面一遍文章讲了通过xml来装配Bean,那么这篇来讲注解方式来讲装配Bean对象 注解方式需要在原先的基础上重新配置环境: (1)Component标签举例 1:导入 ...

  2. spring IOC装配Bean(注解方式)

    1 Spring的注解装配Bean (1) Spring2.5 引入使用注解去定义Bean @Component 描述Spring框架中Bean (2) Spring的框架中提供了与@Componen ...

  3. Spring 框架 详解 (四)------IOC装配Bean(注解方式)

    Spring的注解装配Bean Spring2.5 引入使用注解去定义Bean @Component  描述Spring框架中Bean Spring的框架中提供了与@Component注解等效的三个注 ...

  4. 05_IOC容器装配Bean(注解方式)

    IOC容器装配Bean(注解方式) 1.使用注解方式进行Bean注册 xml 方式: <bean id="" class=""> spring2.5 ...

  5. Spring框架(2)---IOC装配Bean(xml配置方式)

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

  6. Spring 框架 详解 (三)-----IOC装配Bean

    IOC装配Bean: 1.1.1 Spring框架Bean实例化的方式: 提供了三种方式实例化Bean. * 构造方法实例化:(默认无参数) * 静态工厂实例化: * 实例工厂实例化: 无参数构造方法 ...

  7. 04_IOC容器装配Bean(xml方式)

    IOC容器装配Bean(xml方式) 1.Spring 提供配置Bean三种实例化方式 1)使用类构造器实例化(默认无参数) <bean id="bean1" class=& ...

  8. Spring框架---IOC装配Bean

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

  9. Spring总结四:IOC和DI 注解方式

    首先我们要了解注解和xml配置的区别: 作用一样,但是注解写在Bean的上方来代替我们之前在xml文件中所做的bean配置,也就是说我们使用了注解的方式,就不用再xml里面进行配置了,相对来说注解方式 ...

随机推荐

  1. Linux多安全策略和动态安全策略框架演示验证方案及结果分析

    3演示验证方案及结果分析3.1演示验证方案3.1.1验证目标该方案主要用于验证采用Flask体系结构实现的SELinux对系统的防护过程及相应的防护原理.3.1.2验证环境操作系统:启用了SELinu ...

  2. NodeJs + mongodb模块demo

    代码比较通俗易懂,但是我还是在这个过程中浪费了不少时间,也算是看到了nodejs中异步的一个小坑.未来的坑还有很多,慢慢找坑填坑吧. 参考资料如下: 1.断言模块 : https://nodejs.o ...

  3. <<< Tomcat运行报错IOException while loading persisted sessions: java.io.EOFException

    解决方法:将work下面的文件清空,主要是*.ser文件,或者只是删除掉session.ser即可以解决.

  4. Samba服务器配置

    Samba服务器配置流程: (1)安装samba服务器先用#rpm -ivh samba-列出与samba有关的rpm包然后选择第一个包,用tab键补齐文件名 (2)创建新用户和其密码#useradd ...

  5. web前端各大技术都能实现什么功能

    web前端各大技术都能实现什么功能 以下是孜然为你总结的web前端开发你必须要一项一项掌握的技术:Html.css.ajax.jquery.extjs.JavaScript,今天为你详细解读他们各自都 ...

  6. C语言基础(8)-const,volatile,register关键字

    1 const const是定义一个常量 2 volatile 代表定义一个变量,这个变量值可能在CPU指令之外被改变 volatile int a;//定义了一个volatile类型的int变量 通 ...

  7. css之display:inline-block

    display:inline-block: 作用:将对象呈现为inline对象,但是对象的内容作为block对象呈现.之后的内联对象会被排列在同一行内.比如我们可以给一个link(a元素)inline ...

  8. SHLVL 和 BASH_SUBSHELL 两个变量的区别

    SHLVL 是记录多个 Bash 进程实例嵌套深度的累加器,而 BASH_SUBSHELL 是记录一个 Bash 进程实例中多个子 Shell(subshell)嵌套深度的累加器. 看不懂上面这句话不 ...

  9. 0、Web基本概念

    一.Web的概念: 本意是蜘蛛网和网的意思,在网页设计中我们称为网页的意思. 二.Web的分类:Internet上供外界访问的Web资源分为静态Web资源和动态Web资源两种. 1.静态Web资源:W ...

  10. PHP exec/system启动windows应用程序,执行.bat批处理,执行cmd命令

    exec 或者 system 都可以调用cmd 的命令 直接上代码: <?php /** 打开windows的计算器 */ exec('start C:WindowsSystem32calc.e ...