Spring 的两种注入方式:

1. 属性注入:通过无参构造函数+setter方法注入

2. 构造注入:通过有参的构造函数注入。

优缺点:

1. 属性注入直白易懂,缺点是对于属性可选的时候,很多个构造函数会显得类很臃肿。

2. 构造注入是一种高内聚的体现,特别是针对有些属性需要在对象在创建时候赋值,且后续不允许修改(不提供setter方法)。无参构造方法也不能省略。

不是构造的就是用属性注入的方法。上一篇博客就是属性注入。

构造的例子:

public class HelloSpring {
private String name;//定义变量who 他的值通过Spring框架进行注入
//构造注入
public HelloSpring() {
} public HelloSpring(String name) {
this.name = name;
} @Override
public String toString() {
return "HelloSpring{" +
"name='" + name + '\'' +
'}';
}
}

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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--构造注入-->
<bean id="HelloSpring" class="cn.kitty.bean.HelloSpring" >
<constructor-arg index="0" value="kitty"></constructor-arg>
</bean>
</beans>

test

public class Test201710059 {
@Test
public void Test201710059(){
ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
HelloSpring spring = (HelloSpring) context.getBean("HelloSpring");
System.out.println(spring);
}
}

Spring 注入的两种方式的更多相关文章

  1. Spring管理事物两种方式

    Spring管理事物两种方式 1. 编程式事物管理(在开发中不经常使用) 使用步骤 1. 配置数据库事物管理 DataSourceTransactionManager <!--配置事物管理器-- ...

  2. 使用web.xml方式加载Spring时,获取Spring context的两种方式

    使用web.xml方式加载Spring时,获取Spring context的两种方式: 1.servlet方式加载时: [web.xml] <servlet> <servlet-na ...

  3. Spring IOC 依赖注入的两种方式XML和注解

    依赖注入的原理 依赖注入的方式---XML配置 依赖注入的方式---注解的方式 Spring 它的核心就是IOC和AOP.而IOC中实现Bean注入的实现方式之一就是DI(依赖注入). 一 DI的原理 ...

  4. Spring注入值得2种方式:属性注入和构造注入

    Spring是一个依赖注入(控制反转)的框架,那么依赖注入(标控制反转)表现在那些地方了? 即:一个类中的属性(其他对象)不再需要手动new或者通过工厂方法进行创建,而是Spring容器在属性被使用的 ...

  5. Python 防止mysql 注入的两种方式

    Python防止sql注入一般有两种方法 1.escape_string   MySQLdb.escape_string(param) 注意:如果报错出现 'ascii' codec can't en ...

  6. Spring基于注解注入的两种方式

    1.@Autowried 1)默认基于类型查找容器的的Bean进行注入(注入的Bean的实现类是唯一的). 2)当实现类的Bean大于一个的时候,需结合@Qualifier,根据Bean的名称来指定需 ...

  7. Spring自动注入,类型注入、名称注入(两种方式)

    参考: https://blog.csdn.net/qq_41767337/article/details/89002422 https://www.iteye.com/blog/breezylee- ...

  8. 使用aspectJ实现Spring AOP的两种方式

    方式一:基于aspectJ的XML配置 方式二:基于aspectJ的注解方式 基于aspectJ的XML配置 1)       引入相关jar包 2)       创建Spring核心配置文件,必须导 ...

  9. 实现mapper接口注入的两种方式,以及后台的使用区别

    1.使用模板方式: <!--使用模板类实现mybatis --> <bean id="sqlSession" class="org.mybatis.sp ...

随机推荐

  1. shell脚本中 if 判断时候-s是什么意思

    -s file 文件大小非0时为真[ -f "somefile" ] :判断是否是一个文件 [ -x "/bin/ls" ] :判断/bin/ls是否存在并有可 ...

  2. PHP中的回调函数

    回调函数是指调用函数的时候将另一个函数作为参数传递到调用的函数中,而不是传递一个普通的变量作为参数 使用回调函数是为了可以将一段自己定义的功能传到函数内部使用 声明回调函数的方法 变量函数声明 1 2 ...

  3. 微信小程序:wx.navigateBack页面返回传参

    场景(比如:商城选地址,选优惠券): 在[页面A]中调用wx.navigateTo方法跳转到[页面B] 然后从[页面B]返回[页面A], 并将[页面B]中的一些数据传回[页面A] wx.navigat ...

  4. 定位crash的问题

    一般都要符号化crash日志,但是低内存奔溃却没有堆栈日志 A Low Memory report differs from other crash reports in that there are ...

  5. 梯度下降法(BGD、SGD)、牛顿法、拟牛顿法(DFP、BFGS)、共轭梯度法

    一.梯度下降法 梯度:如果函数是一维的变量,则梯度就是导数的方向:      如果是大于一维的,梯度就是在这个点的法向量,并指向数值更高的等值线,这就是为什么求最小值的时候要用负梯度 梯度下降法(Gr ...

  6. cocos2d JS 创建实现换行功能的聊天文本 testLable

    -- 创建实现换行功能的文本 function LoadLabelHeroIntroduce(objLabelContent)  //传入文本 ---------------------------- ...

  7. top结果解释

    top结果解释 top命令功能类似Windows的任务管理器,但不如任务管理器的直观明了,下面对各项内容进行简单介绍. 信息行 top--命令/up-更新时间/users-用户数/load avera ...

  8. laravel架构

    1.Laravel 5.1 中的异常处理器和HTTP异常处理实例教程 http://laravelacademy.org/post/1867.html 2.laravel 集成sentry,sentr ...

  9. java virtual machine launcher Error:Could not create the Java Virtual Machine. Error:A Fatal exception has occurred,Program will exit.

    Error:Could not create the Java Virtual Machine. Error:A Fatal exception has occurred,Program will e ...

  10. STL之Map和multimap容器

    1.Map和multimap容器 1)map是标准的关联式容器,一个map是一个键值对序列,即(key,value)对.它提供基于key的快速检索能力. 2)map中key值是唯一的.集合中的元素按一 ...