`

Spring4.X之Constructor Injection 构造方法注入

    博客分类:
  • S2SH
阅读更多
Spring4.X之Constructor Injection 构造方法注入


With Spring Framework 4.3 it’s very easy to write components that use constructor injections as you no longer need to use @Autowired. As long as you have a single constructor, Spring will implicitly consider it an autowire target:


在 Spring4.x 中增加了新的特性:

如果类只提供了一个带参数的构造方法,则不需要对对其内部的属性写 @Autowired 注解,
Spring 会自动为你注入属性。

注意:
要被注入的Bean,需要在class前写上诸如 @Component,@Service 的注解。

@Component
public class MyComponent {
    
    private final SomeService service;

    public MyComponent(SomeService service) {
        this.service = service;
    }

} 


@Service
public class SomeService {

} 








引用:

https://spring.io/blog/2016/04/15/testing-improvements-in-spring-boot-1-4


































--
分享到:
评论

相关推荐

    Manning.Spring.in.Action.4th.Edition.2014.11.epub

    2.4.3. Initializing a bean with constructor injection 2.4.4. Setting properties 2.5. Importing and mixing configurations 2.5.1. Referencing XML configuration in JavaConfig 2.5.2. Referencing Java...

    反射之操作Constructor 构造方法类分析.md

    反射之操作Constructor 构造方法类分析.md

    spring-framework-reference-4.1.2

    What’s New in Spring Framework 4.x .................................................................................... 16 3. New Features and Enhancements in Spring Framework 4.0 .....................

    spring-framework-reference4.1.4

    What’s New in Spring Framework 4.x .................................................................................... 16 3. New Features and Enhancements in Spring Framework 4.0 .....................

    springAOP demo 带错误解决文档

    Could not instantiate bean class [org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: ...

    Spring_IoC入门笔记.md

    依赖注入:Dependency Injection IOC的作用:降低程序间的偶合(依赖关系) 依赖关系的管理:以后都交给spring来维护 在当前类需要用到其他类的对象时,由spring为我们提供,我们只需在配置文件中说明 依赖关系...

    conga-dependency-injection:用于node.js的控制模块的依赖注入反转

    > npm install conga-dependency-injection 用法 配置实例 var config = { // set up some parameters "parameters" : [ "my.service.constructor" : "path/to/some/service/file", "fs.service.constructor" : ...

    spring 实例的工厂方法 依赖注入属性,都使用property 元素确

    ·工厂方法如果需要参数,都使用constructor-arg属性确定参数值。 ·其他依赖注入属性,都使用property 元素确定参数值。 Person.java PersonFactory.java Chinese.java American.java bean.xml SpringTest.java

    javascript设计模式Constructor(构造器)模式.docx

    javascript设计模式Constructor(构造器)模式.docx

    spring applicationContext 配置文件

    <bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean" p:dataSource-ref="dataSourceProxy"> <value>classpath:SqlMapConfig.xml</value> </property> ...

    Java跳过构造方法新建对象

     (2) 运用反射手段,调用java.lang.Class或者java.lang.reflect.Constructor类的newInstance()实例方法。  (3) 调用对象的clone()方法。  (4) 运用反序列化手段,调用java.io.ObjectInputStream对象的 ...

    JAVA核心知识点整理(有效)

    2.2.3. 本地方法区(线程私有) ................................................................................................................ 23 2.2.4. 堆(Heap-线程共享)-运行时数据区 .....................

    springCloud.docx

    这是spring cloud的教程,如果你还不会用springcloud,把这个下载下来学一学就全会了。

    myspringlearning:Spring学习项目

    Spring学习项目IOC依赖的jar包 aspect2 dbcp hibernate jdom junit4 mysql pool2 spring slf4j注入类型setter(重要)Setter-based dependency injection构造方法注入 Constructor-based dependency injectionbeans....

    S2SH整合报错

    配置了日志之后打印出下列信息: ERROR main org.springframework.web.context.... at java.lang.Class.getConstructor0(Class.java:2640) at java.lang.Class.getDeclaredConstructor(Class.java:1953) ……

    JavaScript的constructor属性[文].pdf

    JavaScript的constructor属性[文].pdf

    java反射例子

    i < constructor.length; i++) { System.out.println("构造方法:"+constructor[i]); } //方法二 for (int i = 0; i < constructor.length; i++) { Class<?> p[]=constructor[i].getParameterTypes(); ...

    Spring的学习笔记

    (二) 构造方法Constructor Injection 10 四、 id、name 11 五、 简单属性的注入 11 六、 Bean的作用范围scope 12 七、 集合注入 12 八、 自动装配autowire 13 (一) byName 13 (二) byType 14 (三) 注意 14 九、 生命...

    EJB3.0实例教程

    1.2 联系作者.....................................................................................................................................................................4 第二章运行环境配置......

    Java 面试宝典

    19、构造器 Constructor 是否可被 override? ................................................................ 15 20、接口是否可继承接口? 抽象类是否可实现(implements)接口? 抽象类是否可继 承具体类...

Global site tag (gtag.js) - Google Analytics