Spring设置注入和构造注入详解
来源:互联网 发布:js获取文本框的值 编辑:程序博客网 时间:2024/05/16 10:41
在Spring中注入方式有设置注入和构造注入。设置注入就是指要被注入的类中定义有一个setter()方法,并在参数中定义需要注入的对象。简单的看个例子。
定义有User类,并覆写了toString()方法。
- package com.zcl.spring.setterinjection;
- public class User {
- private String name ;
- private int age ;
- private String country ;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public String getCountry() {
- return country;
- }
- public void setCountry(String country) {
- this.country = country;
- }
- public String toString(){
- return name + " is " + age + " years old,living in " + country ;
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans">
- <bean id="user" class="com.zcl.spring.setterinjection.User">
- <property name="name" value="Zhao" />
- <property name="age" value="22" />
- <property name="country" value="China" />
- </bean>
- </beans>
测试一下:
- package com.zcl.spring.setterinjection;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- public class Main {
- public static void main(String args[]){
- ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml") ;
- User user = (User)context.getBean("user") ;
- System.out.println(user) ;
- }
- }
输入结果:
- Zhao is 22 years old,living in China
现在我们来看写构造注入,所谓构造注入就是指要被注入的类中声明一个构造方法,并在此方法的参数中定义要注入的对象。修改下刚刚的User类。
- package com.zcl.spring.setterinjection;
- public class User {
- private String name ;
- private int age ;
- private String country ;
- public User(String name, int age, String country) {
- this.name = name;
- this.age = age;
- this.country = country;
- }
- public String toString(){
- return name + " is " + age + " years old,living in " + country ;
- }
- }
配置beans.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans.xsd"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns="http://www.springframework.org/schema/beans">
- <bean id="user" class="com.zcl.spring.setterinjection.User">
- <constructor-arg value="Zhao" />
- <constructor-arg value="22" />
- <constructor-arg value="China" />
- </bean>
- </beans>
测试函数一样,打印结果也一样。
ref元素是用在property中,来设置需要引用的容器管理的其它Bean。
它的用法:<ref bean|local|parent="someBean">,这里主要分析一下这三个参数的作用。
这次先看实例,再进行讲解。
· 先建立一个包:javamxj.spring.basic.ref ,然后把以下5个文件放在这个包下。
HelloBean.java
- package javamxj.spring.basic.ref;
- public class HelloBean {
- private String hello;
- public String getHello() {
- return hello;
- }
- public void setHello(String hello) {
- this.hello = hello;
- }
- }
HelloDate.java
- package javamxj.spring.basic.ref;
- import java.util.Date;
- public class HelloDate {
- private Date date;
- private HelloBean hb;
- public void setDate(Date date) {
- this.date = date;
- }
- public void setHb(HelloBean hb) {
- this.hb = hb;
- }
- public void sayHello() {
- System.out.println(hb.getHello() + " " + date.toLocaleString());
- }
- }
beans.xml
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">
- <property name="hello" value="Hello! Child Bean." />
- </bean>
- <bean id="dateBean" name="#date" class="java.util.Date"/>
- <bean id="hd1" class="javamxj.spring.basic.ref.HelloDate">
- <property name="hb">
- <ref bean="helloBeanParent"/>
- </property>
- <property name="date">
- <ref bean="#date"/>
- <!--<ref bean="dateBean"/>-->
- </property>
- </bean>
- <bean id="hd2" class="javamxj.spring.basic.ref.HelloDate">
- <property name="hb">
- <ref local="helloBean"/>
- </property>
- <property name="date">
- <ref local="dateBean"/>
- </property>
- </bean>
- <bean id="hd3" class="javamxj.spring.basic.ref.HelloDate">
- <property name="hb">
- <ref parent="helloBean"/>
- </property>
- <property name="date">
- <bean class="java.util.Date"/>
- </property>
- </bean>
- </beans>
parent.xml
- <?xml version="1.0" encoding="GBK"?>
- <!DOCTYPE beans public "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <bean id="helloBean" class="javamxj.spring.basic.ref.HelloBean">
- <property name="hello" value="Hello! Javamxj." />
- </bean>
- <bean id="helloBeanParent" class="javamxj.spring.basic.ref.HelloBean">
- <property name="hello" value="Hello! Parent Bean." />
- </bean>
- </beans>
Main.java
- package javamxj.spring.basic.ref;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.xml.XmlBeanFactory;
- import org.springframework.core.io.ClassPathResource;
- public class Main {
- public static void main(String[] args) {
- BeanFactory parent = new XmlBeanFactory(new ClassPathResource(
- "javamxj/spring/basic/ref/parent.xml"));
- BeanFactory child = new XmlBeanFactory(new ClassPathResource(
- "javamxj/spring/basic/ref/beans.xml"), parent);
- HelloDate hd1 = (HelloDate) child.getBean("hd1");
- HelloDate hd2 = (HelloDate) child.getBean("hd2");
- HelloDate hd3 = (HelloDate) child.getBean("hd3");
- hd1.sayHello();
- hd2.sayHello();
- hd3.sayHello();
- }
- }
·运行Main.java,输出结果如下:
Hello! Parent Bean. 2005-8-10 22:25:56
Hello! Child Bean. 2005-8-10 22:25:56
Hello! Javamxj. 2005-8-10 22:25:56
OK!这里主要分析beans.xml、Main.java这两个文件。对于Main.java要注意的是如何加载“parent”的,重点看看beans.xml中ref元素的用法。
首先定义两个bean:helloBean、dateBean,分别指向HelloBean类和Date类。然后定义了hd1、hd2、hd3等三个bean,都指向HelloDate类。
·hd1:
property标签中的“helloBeanParent”并不存在于beans.xml中,而是在parent.xml中,使用<ref bean="someBean">可以从其它bean配置文件中寻找需要加载的目标bean。bean属性的值可以同目标bean的id属性相同,也可以同它的name属性中任意的一个值相同。
·hd2:
property标签中的“helloBean”如果不存在于beans.xml中,则XML解析器会提示错误。<ref local="someBean">只能这个bean配置文件中寻找需要加载的目标bean,而且local属性值必须同目标bean的id属性相同。
·hd3:
property标签中的“helloBean”同时存在于beans.xml和parent.xml中,使用<ref bean="someBean">则会优先从beans.xml中寻找需要加载的目标bean,如果需要从parent.xml中加载目标bean,则需使用<ref parent="someBean">。在设置date时,使用的是内联bean,这时可以不要任何id或name定义。
spring idref和ref的区别
idref元素用来将容器内其它bean的id传给<constructor-arg/> 或 <property/>元素,同时提供错误验证功能。
<bean id="theTargetBean" class="..."/>
<bean id="theClientBean" class="...">
<property name="targetName">
<idref bean="theTargetBean" />
</property>
</bean>
上述bean定义片段完全地等同于(在运行时)以下的片段
<bean id="theTargetBean" class="..." />
<bean id="client" class="...">
<property name="targetName" value="theTargetBean" />
</bean>
也是就是说idref我可以获取spring容器中的bean的name的值(一个字符串),而不是bean的实例。
idref元素的功能与<value>类似,就是idref多了验证的功能,减少配置的书写错误机率。除了<idref bean=""/>,如果被引用的bean在同一个xml文件中,且bean的名字就是bean的id,除了可以使用<idfef local=""/>,此属性允许xml解析器在解析XML的时候对引用的bean进行验证。
而value方式,传给client bean的targetName属性值并没有被验证。任何的输入错误仅在client bean实际实例化时才会被发现(可能伴随着致命的错误)。
而ref是获取这个bean的实例。用来实现注入功能。
假如只是想获取bean的名称 采用idref
使用idref标记允许容器在部署时 验证所被引用的bean是否存在。
ref有三个属性:local、parent、bean,具体区别如下:
- local只能指定与当前配置的对象在同一个配置文件的对象定义的名称;
- parent则只能指定位于当前容器的父容器中定义的对象引用;
- bean则基本上通吃,所以,通吃情况下,直接使用bean来指定对象引用就可以了。
- Spring设置注入和构造注入详解
- Spring设置注入和构造注入
- Spring设置注入和构造注入
- Spring设置注入和构造注入
- Spring设置注入和构造注入
- Spring 构造注入和设置注入
- spring注入:构造注入
- 了解Spring 构造注入和自动注入
- Spring 属性注入和构造器注入
- 【Spring】设值注入和构造注入
- Spring的注入方式:设值注入和构造注入
- Spring属性注入和构造器注入、工厂方法注入
- 记录一下spring的设置注入 构造器注入
- Spring注入:设值注入、构造注入
- Spring内核研究-set方法注入和构造函数注入
- Spring依赖属性注入和构造函数注入
- Spring内核研究-set方法注入和构造函数注入
- Spring设值注入和构造注入的比较
- MATLAB 生成随机整数 的各种方法以及实例
- 基础练习 Huffman树
- 使用jdbc和javabean操作数据库
- html的meta标签
- lua学习:lua中“类”的实现
- Spring设置注入和构造注入详解
- Linux Qt下简单的多线程编程
- iOS开发UI篇—控制器的View的创建
- VirtualBox简体中文版下载安装使用图解教程
- 基于Devstack 开发环境下Reboot Host之后的各个服务的启动过程
- DDMS analysize 方法
- 求素数: 一般线性筛法 + 快速线性筛法
- js强制要求保留两位小数
- LVS调度方式