bean属性注入

(一)构造方法的属性注入

1.Student.java

package entity;

public class Student {
private String name;
private String age; public Student(String name, String age) {
this.name = name;
this.age = age;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
'}';
}
}

2.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="student" class="entity.Student"> <constructor-arg name="name" value="李四"/> <constructor-arg name="age" value="21"/> </bean> </beans>

3.StudentTest.java

package Test;

import entity.Student;

import entity.User;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest {

    @Test

    public void stu(){

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        Student student=(Student)applicationContext.getBean("student");

        System.out.println(student);

    }

}
(二)set方法的属性注入

1.Student.java

package entity;

public class Student {

    private String name;

    private String age;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getAge() {

        return age;

    }

    public void setAge(String age) {

        this.age = age;

    }

    @Override

    public String toString() {

        return "Student{" +

                "name='" + name + '\'' +

                ", age='" + age + '\'' +

                '}';

    }

}

2.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="student2" class="entity.Student">

        <property name="name" value="张三"/>

        <property name="age" value="22"/>

    </bean>

</beans>

3.StudentTest.java

package Test;

import entity.Student;

import entity.User;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest {

      @Test

    public void stu2(){

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        Student student2=(Student)applicationContext.getBean("student2");

        System.out.println(student2);

    }

}
(三)p名称空间的属性注入

1.Student.java

package entity;

public class Student {

    private String name;

    private String age;

    private Cat cat;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getAge() {

        return age;

    }

    public void setAge(String age) {

        this.age = age;

    }

    public Cat getCat() {

        return cat;

    }

    public void setCat(Cat cat) {

        this.cat = cat;

    }

    @Override

    public String toString() {

        return "Student{" +

                "name='" + name + '\'' +

                ", age='" + age + '\'' +

                ", cat=" + cat +

                '}';

    }

}

2.Cat.java

package entity;

public class Cat {

    private String name;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String toString() {

        return "Cat{" +

                "name='" + name + '\'' +

                '}';

    }

}

3.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:p="http://www.springframework.org/schema/p"

       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="student3" class="entity.Student" p:name="小明" p:age="15" p:cat-ref="cat"/>

    <bean id="cat" class="entity.Cat" p:name="小黄"/>

</beans>

4.StudentTest.java

package Test;

        import entity.Student;

        import entity.User;

        import org.junit.Test;

        import org.springframework.context.ApplicationContext;

        import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest {

    @Test

    public void stu3(){

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        Student student3=(Student)applicationContext.getBean("student3");

        System.out.println(student3);

    }

}
(四)spel的属性注入

1.Student.java

package entity;

public class Student {

    private String name;

    private String age;

    private Cat cat;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    public String getAge() {

        return age;

    }

    public void setAge(String age) {

        this.age = age;

    }

    public Cat getCat() {

        return cat;

    }

    public void setCat(Cat cat) {

        this.cat = cat;

    }

    @Override

    public String toString() {

        return "Student{" +

                "name='" + name + '\'' +

                ", age='" + age + '\'' +

                ", cat=" + cat +

                '}';

    }

}

2.Cat.java

package entity;

public class Cat {

    private String name;

    public String getName() {

        return name;

    }

    public void setName(String name) {

        this.name = name;

    }

    @Override

    public String toString() {

        return "Cat{" +

                "name='" + name + '\'' +

                '}';

    }

}

3.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:p="http://www.springframework.org/schema/p"

       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="student4" class="entity.Student">

        <property name="name" value="#{'大名'}"/>

        <property name="age" value="#{16}"/>

        <property name="cat" value="#{cat}"/>

    </bean>

    <bean id="cat" class="entity.Cat">

        <property name="name" value="#{'大黄'}"/>

    </bean>

</beans>

4.StudentTest.java

package Test;

        import entity.Student;

        import entity.User;

        import org.junit.Test;

        import org.springframework.context.ApplicationContext;

        import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentTest {

    @Test

    public void stu4(){

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        Student student4=(Student)applicationContext.getBean("student4");

        System.out.println(student4);

    }

}
(五)复杂类型的属性注入

1.Bean.java

package entity;

import java.util.*;

public class Bean {

    private String arr[];//数组类型

    private List<String > list;//List集合类型

    private Set<String> set;//Set集合类型

    private Map<String,Integer> map;//Map集合类型

    private Properties properties;//属性类型

    public String[] getArr() {

        return arr;

    }

    public void setArr(String[] arr) {

        this.arr = arr;

    }

    public List<String> getList() {

        return list;

    }

    public void setList(List<String> list) {

        this.list = list;

    }

    public Set<String> getSet() {

        return set;

    }

    public void setSet(Set<String> set) {

        this.set = set;

    }

    public Map<String, Integer> getMap() {

        return map;

    }

    public void setMap(Map<String, Integer> map) {

        this.map = map;

    }

    public Properties getProperties() {

        return properties;

    }

    public void setProperties(Properties properties) {

        this.properties = properties;

    }

    @Override

    public String toString() {

        return "Bean{" +

                "arr=" + Arrays.toString(arr) +

                ", list=" + list +

                ", set=" + set +

                ", map=" + map +

                ", properties=" + properties +

                '}';

    }

}

2.applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:p="http://www.springframework.org/schema/p"

       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="bean" class="entity.Bean">

        <!--数组类型-->

        <property name="arr">

            <list >

                <value>aaa</value>

                <value>bbb</value>

                <value>ccc</value>

            </list>

        </property>

        <!--List集合类型-->

        <property name="list">

            <list>

                <value>111</value>

                <value>222</value>

                <value>333</value>

            </list>

        </property>

        <!--Set集合类型-->

        <property name="set">

            <set>

                <value>aaa</value>

                <value>bbb</value>

                <value>ccc</value>

            </set>

        </property>

        <!--Map集合类型-->

        <property name="map">

            <map>

                <entry key="aaa" value="111"/>

                <entry key="bbb" value="222"/>

                <entry key="ccc" value="333"/>

            </map>

        </property>

        <!--属性类型-->

        <property name="properties">

            <props>

                <prop key="username">111</prop>

                <prop key="password">222</prop>

            </props>

        </property>

    </bean>

</beans>

3.BeanTest.java

package Test;

import entity.Bean;

import org.junit.Test;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class BeanTest {

    @Test

    public void bean(){

        ApplicationContext applicationContext=new ClassPathXmlApplicationContext("applicationContext.xml");

        Bean bean=(Bean)applicationContext.getBean("bean");

        System.out.println(bean);

    }

}
 

spring学习笔记之---bean属性注入的更多相关文章

  1. Spring学习笔记(3)——Bean的注入方式

    依赖注入 依赖注入支持属性注入.构造函数注入.工厂注入. 属性注入: 属性注入即通过setXxx()方法注入Bean的属性值或依赖对象 属性注入要求Bean提供一个默认的构造函数(无参构造函数),并为 ...

  2. Spring学习笔记—装配Bean

    在Spring中,对象无需自己负责查找或创建与其关联的其他对象.相反,容器负责把需要相互协作的对象引用赋予各个对象.创建应用对象之间协作关系的行为通常称为装配(wiring),这也是依赖注入的本质. ...

  3. spring学习(三)属性注入

    用的是IDEA的maven工程,pom.xml文件导包依赖省略 本文主要写set方式注入 (一).一般类型注入 一.写两个实体类Car.User public class Car { private ...

  4. spring学习笔记之---bean管理的注解方式

    bean管理的注解方式 (一)使用注解定义bean (1)常用注解 (2)实例 1.在pom.xml中进行配置 <dependencies> <dependency> < ...

  5. Spring学习笔记之Bean的一些属性设置

    1.beans 里边配置default-init-method="shunge",有这个方法的会执行,没有也不会报错 2.beans 里边配置default-destroy-met ...

  6. Spring学习笔记(2)——Bean的配置

    要使应用程序中的Spring容器成功启动,需要以下三个方面的条件都具备: 1.Spring框架的类包都已经放到应用程序的类路径下 2.应用程序为Spring提供完备的Bean配置信息 3.Bean的类 ...

  7. Spring学习笔记之bean配置

    1.命名bean 每个bean都有一个或者多个的的标识符.这些标识符必须在加载他们的容器里边唯一.一个bean经常有且只有一个标识符,但是如果需要超过一个的名字,可以考虑额外的别名. 基于xml的配置 ...

  8. Spring学习笔记之Bean的实例化

    一.bean的实例化方法有3种, 1.构造器实例化 2.静态工厂方法实例化 3.实例工厂方法实例化 二.用构造器来实例化 <bean id="ShunDao" class=& ...

  9. Spring学习笔记——02 Bean的命名及实例化

    一.Bean的命名 前一篇讲到IoC是一个管理Bean的容器,Bean多数情况下都是通过XML文件进行配置的,其中Bean的命名有以下几种方式,现在梳理一下. 1. 不指定id,只配置类名 <b ...

随机推荐

  1. Android 上传开源项目到 jcenter 实战踩坑之路

    本文微信公众号「AndroidTraveler」首发. 背景 其实 Android 上传开源项目到 jcenter 并不是一件新鲜事,网上也有很多文章. 包括我本人在将开源项目上传到 jcenter ...

  2. RABC权限控制(页面操作角色,权限和进行分配)

    上一节主要说的是如何通过url进行权限控制,这一节就说一下如何开发一个权限控制的界面,这样我们就能很方便的创建角色,并分配给用户不同角色和不同权限. 1.编写角色管理页面 这个编写较为简单,主要是通过 ...

  3. 一套简单的web即时通讯——第三版

    前言 接上版,本次版本做了如下优化: 1.新增同意.拒绝添加好友后做线上提示: 2.新增好友分组,使用工具生成后台API,新增好友分组功能,主要功能有:添加分组.重命名分组名称.删除分组 3.新增好友 ...

  4. HDU 4059:The Boss on Mars(数学公式+容斥原理)

    http://acm.hdu.edu.cn/showproblem.php?pid=4059 题意:给出一个n,求1~n里面与n互质的数的四次方的和是多少. 思路:不知道1~n的每个数的四次方的求和公 ...

  5. Tensorflow教程(1)Tensorflow的下载和安装

    人工智能已经成为了目前的大趋势,作为程序员的我们也应该跟着时代进步.Tensorflow作为人工智能领域的重要工具,被广泛的使用在机器学习的应用当中. Tensorflow使用人数众多.社区完善,所以 ...

  6. Mysql中varchar和char区别

    一.varchar和char的区别: 区别一:定长和变长 char表示定长.长度固定,varchanr表示变长,即长度可变. 即char类型是规定多少字长则必须存储多少字长,超过的长度的字段则只能截取 ...

  7. Ray聊天记录

    由于工作变动,Ray的文档.示例没有及时更新,深表歉意.在Ray升级后,性能较几个月前有了非常大的提升,也更具易用性.这是QQ交流群里大家的聊天记录,跟大家分享一下(由于时间仓促群里大家的聊天记录没有 ...

  8. 《C Primer Plus(第6版)中文版》勘误

    搬运自己2016年11月28日发布于SegmentFault的文章.链接:https://segmentfault.com/a/1190000007626460 本勘误由本人整理并发布,仅针对下方列出 ...

  9. Vmware centos7无法联网的问题解决

    VMware三种网络连接方式的区别 : 1) bridge : 默认使用VMnet0,不提供DHCP服务 在桥接模式下,虚拟机和宿主计算机处于同等地位,虚拟机就像是一台真实主机一样存在于局域网中.因此 ...

  10. Hyperledger Fabric 之 Channel ,创建channel链接几项注意点

    好长时间没有更新博客,网上也有很多fabric的部署资料,而且也都很不错,也比较全面.我就再想重复的工作暂时就不用做了,后面抽时间在做细化和分类:就将学习和工作中遇到和解决的问题经验,做一些分享. 而 ...