Spring IoC容器会先把所有的Bean都进行实例化,不管是要用到的火鼠用不到的,如果你想暂时不进行Bean的实例化,要用到属性
lazy-init="true".

Spring的三种注入方式:

① 构造注入:通过构造器(constructor)注入

② 设值注入:通过Setter方法注入

③ 反射注入:通过注解(annotation)的方式注入

Spring 对Bean的四种自动装配(注入)方式

autowire= "byName" :通过Bean的名字对属性进行值注入

autowire="byType":通过属性的类型来对属性进行值注入。<慎用>

autowire="constructor":通过构造器来对属性进行值注入。<慎用>

autowire="autodetect":容器自动对属性进行值注入,先用constructor的方式,如果没有构造器,再用byType的方式。<尽量不用>

通过注解的方式对Bean的自动注入:

@Autowired :是通过"byType"的方式对Bean属性进行自动注入的,如果Bean属性的类型有多个,那么就添加@Qualifier("beanName") 加以区分。

@Resource:是通过"byType"的方式对Bean属性进行自动注入的,如果Bean属性的类型有多个,那么就用@Resource("beanName") ,

@Resource("beanName") 是通过"byName"的方式对Bean属性进行自动注入的。

Spring Bean的应用范围

scope="singleton":单例(默认),对所有应用都只生成一个实例

scope="prototype":对每个应用都生成一个实例

scope="request":在web应用中有效,对每个请求都生成一个实例

scope="session":在web应用中有效,对每个会话都生成一个实例

scope="global-session":在web应用中有效,全局Http会话

Spring的IoC组件

@Repository:持久层组件,用于标注数据访问层组件,如DAO层组件。

@Service:服务成组件,用于标注业务层组件,如Service层组件;会根据Bean的类型实例化一个首字母为小写的bean的实例,如果要修改bean name可以在@Service("custome beanName")。

@Controller:用于标注控制层主键,如Strust的Action层组件。

@Component:泛指组件,当组件不好归类的时候可以用这个标注。

当用了上面的annotation的时候就不需要再在applicationContext.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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<!--全注解-->
          <context:annotation-config />
          <context:component-scan base-package="com.demo.service" />
          <context:component-scan base-package="com.demo.dao" />
          <context:component-scan base-package="com.demo.controller" />
   
         <tx:annotation-driven transaction-manager="transactionManager"/>
 
        <bean id="sessionFactory"  class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
                   <property name="configLocation" value="classpath:hibernate.cfg.xml" /> 
                   <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>

<!-- 定义事务管理器(声明式的事务) --> 
        <bean id="transactionManager"  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                   <property name="sessionFactory" ref="sessionFactory" />
        </bean>
   
        <bean id="hibernateTempate"  class="org.springframework.orm.hibernate3.HibernateTemplate">
                     <property name="sessionFactory" ref="sessionFactory"/>
        </bean>
</beans>

public class Customer {

private Long customerId;

private String name;

//省略getter 和 setter

}

@Repository("customerDAO")
public class CustomerDAO {

   @Resource
    private HibernateTemplate hibernateTemplate;
 
           public Customer findByPrimaryKey(long customerId) {
                     return (Customer) hibernateTemplate.get(Customer.class, customerId);
           }
 
           public void save(Customer customer) {
                     hibernateTemplate.save(customer);
           }
 
          public void update(Customer customer) {
                    hibernateTemplate.update(customer);
          } 
}

@Service
@Transactional(readOnly=true)
public class CustomerService {

   @Autowired
    @Qualifier("customerDAO")
    private CustomerDAO customerDAO;
 
    public Customer findByPrimaryKey(long customerId) {
        return customerDAO.findByPrimaryKey(customerId);
    }
  
    @Transactional(propagation=Propagation.REQUIRED)
    public void save(Customer customer) {
       customerDAO.save(customer);
    }
 
    @Transactional(propagation=Propagation.REQUIRED)
    public void update(Customer customer) {
       customerDAO.update(customer);
    }
 }

@Controller
public class CustomerController {
 
    @Resource
    CustomerService customerService;

    public void modifyCustomerAndProduct() {
  
                 Customer customer = customerService.findByPrimaryKey(1);
                 customer.setName("joe");
                 customerService.update(customer);

}

}

Spring 反射注入+全注解注入的更多相关文章

  1. Spring构造器注入、set注入和注解注入

    记得刚开始学spring的时候,老师就反复的提到依赖注入和切面,平常的java开发中,在某个类中需要依赖其它类的方法,则通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,sp ...

  2. Spring学习笔记1—依赖注入(构造器注入、set注入和注解注入)

    什么是依赖注入 在以前的java开发中,某个类中需要依赖其它类的方法时,通常是new一个依赖类再调用类实例的方法,这种方法耦合度太高并且不容易测试,spring提出了依赖注入的思想,即依赖类不由程序员 ...

  3. Spring依赖注入:注解注入总结

    更多11   spring   依赖注入   注解   java 注解注入顾名思义就是通过注解来实现注入,Spring和注入相关的常见注解有Autowired.Resource.Qualifier.S ...

  4. Spring依赖注入:注解注入

    注解注入顾名思义就是通过注解来实现注入, Spring和注入相关的常见注解有Autowired.Resource.Qualifier.Service.Controller.Repository.Com ...

  5. 【Spring 基础】通过注解注入Bean

    原课程:通过注解注入Bean 注入bean知识点思维导图 Spring 4.x推荐使用基于构造器的方式进行bean注入7.4.1 Dependency Injection spring为什么推荐使用构 ...

  6. Spring RESTful + Redis全注解实现恶意登录保护机制

    好久没更博了... 最近看了个真正全注解实现的 SpringMVC 博客,感觉很不错,终于可以彻底丢弃 web.xml 了.其实这玩意也是老东西了,丢弃 web.xml,是基于 5.6年前发布的 Se ...

  7. Spring基础知识1--环境搭建、bean创建、依赖注入、注解注入

    一.Spring两大核心内容 1.控制反转IOC/DI:  应用本身不负责对象的创建和维护,对象和依赖对象的创建完全交给容器管理. 2.AOP(面向切面编程):通过预编译的方式,在运行期通过动态代理的 ...

  8. id、name、setter方法注入、构造方法注入、工厂方法注入、注解注入、方法注入、方法替换、Web作用域、普通bean引用Web作用域的bean

    spring IoC的id和name id的命名需要满足XML对id的命名规范,必须以字母开始,后面可以是字母.数字.连字符.下画线.句号.冒号等等号,但逗号和空格是非法的.如果用户确实希望用一些特殊 ...

  9. 详解spring boot mybatis全注解化

    本文重点介绍spring boot mybatis 注解化的实例代码 1.pom.xml //引入mybatis <dependency> <groupId>org.mybat ...

随机推荐

  1. hdu 5120(求两个圆环相交的面积 2014北京现场赛 I题)

    两个圆环的内外径相同 给出内外径 和 两个圆心 求两个圆环相交的面积 画下图可以知道 就是两个大圆交-2*小圆与大圆交+2小圆交 Sample Input22 30 00 02 30 05 0 Sam ...

  2. Two Seals codeforces 837c

    Two Seals 一个矩形a*b,若干子矩形,子矩形中选2个,不重叠能覆盖最大 思路: 枚举: 代码: #include <cstdio> #include <cstring> ...

  3. 【LOJ】#2537. 「PKUWC2018」Minimax

    题解 加法没写取模然后gg了QwQ,de了半天 思想还是比较自然的,线段树合并的维护方法我是真的很少写,然后没想到 很显然,我们有个很愉快的想法是,对于每个节点枚举它所有的叶子节点,对于一个叶子节点的 ...

  4. Caffe训练AlexNet网络,精度不高或者为0的问题结果

    当我们使用Caffe训练AlexNet网络时,会遇到精度一值在低精度(30%左右)升不上去,或者精度总是为0,如下图所示: 出现这种情况,可以尝试使用以下几个方法解决: 1.数据样本量是否太少,最起码 ...

  5. Django实战(13):在session中保存购物车

    现在,我们有了一个产品目录界面,用户如果看到满意的产品,就可以将其放入购物车.下面就让我们来实现购物车的功能. 首先要做一下简单的分析和设计.购物车应该显示一系列产品的清单,其中列出了买方选中的产品. ...

  6. .NET Core2.1下采用EFCore比较原生IOC、AspectCore、AutoFac之间的性能

    一.前言 ASP.NET Core本身已经集成了一个轻量级的IOC容器,开发者只需要定义好接口后,在Startup.cs的ConfigureServices方法里使用对应生命周期的绑定方法即可,常见方 ...

  7. iOS Sprite Kit教程之场景的设置

    iOS Sprite Kit教程之场景的设置 Sprite Kit中设置场景 在图2.8所示的效果中,可以看到新增的场景是没有任何内容的,本节将讲解对场景的三个设置,即颜色的设置.显示模式的设置以及测 ...

  8. keystone 认证深度研究分析

    一.Keystone Token深度概述 Keystone作为OpenStack项目基础认证模块,目前支持的token类型分别是uuid.pkiz.pki.fernet. 首先,简要叙述一下这四种类型 ...

  9. Web2.0应用程序的7条原则

    个人看好Web的发展潜力,本文字摘自<Collective Intelligence 实战> 网络是平台 使用传统许可模式软件的公司或用户必须运行软件.定期更新至最新版本,以及扩展它来满足 ...

  10. bzoj 3996 最小割

    公式推出来后想了半天没思路,居然A是01矩阵..... 如果一个问题是求最值,并那么尝试先将所有可能收益加起来,然后矛盾部分能否用最小割表达(本题有两个矛盾,第一个是选还是不选,第二个是i,j有一个不 ...