配置 bean上

2018年02月15日 14:14 | 2975次浏览

上一章讲解了spring的HelloWorld,现在我们开始讲解如何配置spring的bean。在此之前我们先要了解spring的IOC和DI。

IOC:控制反转,我的理解是把资源准备好等你来拿,而不是需要时在去new对象

DI:依赖注入,我的理解是通过setter方法给属性赋值

接下来我们来讲讲HelloWorld案例

beans.xml文件

<!-- 配置一个 bean -->  
    <bean id="helloWorld" class="com.spring.hello.HelloWorld">  
        <!-- 为属性赋值 -->  
        <property name="hello" value="Hello World"></property>  
    </bean>

bean中有一个id,且id是唯一的。若不指名则为该类的类名并首字母小写。

property 中有一个name,其name就是生产了setter方法的属性,value便是其值。

实例化Spring IOC容器

Spring IOC 容器在读取bean的配置文件之前要将其实例化

//1. 创建Spring 的IOC容器  
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");  
          
        //2. 从容器中获取 Bean 其实就是new 的过程  
        HelloWorld helloWorld = (HelloWorld) ctx.getBean("helloWorld");  
        // 也可以是 HelloWorld helloWorld = ctx.getBean(HelloWorld.class);  
          
        //3. 执行函数  
        helloWorld.helloWorld();

实例化Spring IOC 容器用到 ApplicationContext ,其实现类主要有ClassPathXmlApplicationContext();从类路径加载配置文件。还有个是FileSystemXmlApplicationContext();从文件系统加载配置文件

通过ApplicationContext的getBean方法获取bean;这样就new完了。


直奔主题:配置bean

依赖注入:主要有两个注入方法,一个是属性注入,另一个是构造方法注入。属性注入上面有讲,这里忽略。主要讲解构造注入。

先创建一个实体类User

package com.spring.entity;

public class User {

	private Integer id;
	private String name;
	private String password;

	public User() {
	}

	public User(Integer id, String name, String password) {
		this.id = id;
		this.name = name;
		this.password = password;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", password=" + password
				+ "]";
	}

}

构造方法注入

<bean id="user" class="com.spring.entity.User">
		<constructor-arg value="1"></constructor-arg>
		<constructor-arg value="ITDragon"></constructor-arg>
		<constructor-arg value="此帐号没有密码"></constructor-arg>
	</bean>

读者是否发现了一个问题,构造方法注入怎么没有了name,不要慌!!!默认是按照构造器变量顺序来赋值,也可以通过index,和type自定义顺序,但是笔者觉得没有必要,只是给自己添加麻烦。

测试方法:

private ApplicationContext ctx = null;
	
	{
		ctx = new ClassPathXmlApplicationContext("beans.xml");
	}
	
	@Test
	public void ConstructorDI(){
		User user = ctx.getBean(User.class);
		System.out.println("依赖注入 之 构造方法注入 : "+user.toString());;
	}

测试结果:

依赖注入 之 构造方法注入 : User [id=1, name=ITDragon, password=此帐号没有密码]

特殊符号

如果输入的内容有特殊符号,可以考虑使用<![CDATA[]]> 比如"<","%"等,输入直接保存,读者可以自己试试,解决方法就是<![CDATA[<%ITDragon]]>将其包起来。


引用其他bean

当需要应用到其他bean时可以使用ref

先创建一个需要调用User类的Main类:

package com.spring.entity;

public class Main {
	
	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
	public void hello(){
		System.out.println("应用ref : "+user.toString());
	}
	
}

配置bean

<bean id="main" class="com.spring.entity.Main">  
        <property name="user" ref="user"></property>  
    </bean>

测试方法:

@Test  
    public void RefBean(){  
        Main main = ctx.getBean(Main.class);  
        main.hello();  
    }

测试结果:

应用ref : User [id=1, name=ITDragon, password=此帐号没有密码]

集合属性

创建一个公司类:

package com.spring.entity;

import java.util.List;

public class Company {

	private List<User> users;

	public List<User> getUsers() {
		return users;
	}

	public void setUsers(List<User> users) {
		this.users = users;
	}

	@Override
	public String toString() {
		return "Company [users=" + users + "]";
	}

}

配置bean,顺带把p命名空间也用上了

<!-- 属性注入 p命名-->
	<bean id="user1" class="com.spring.entity.User" 
		p:id="2"
		p:name="这不是昵称"
		p:password="这是p命名空间"
	/>
	
	<!-- List集合 -->
	<bean id="userList" class="com.spring.entity.Company">
		<property name="users">
			<list>
				<ref bean="user"/>
				<ref bean="user1"/>
			</list>
		</property>
	</bean>

测试方法:

@Test  
    public void ListAndP(){  
        Company companys = (Company) ctx.getBean("userList");  
        System.out.println(companys.toString());  
    }

测试结果:

Company [users=[User [id=1, name=ITDragon, password=此帐号没有密码], User [id=2, name=这不是昵称, password=这是p命名空间]]]

Map也是一样,笔者就自己试试,不做太多描述。


---------------------本文来源http://blog.csdn.net/qq_19558705/article/details/49994191



小说《我是全球混乱的源头》

感觉本站内容不错,读后有收获?小额赞助,鼓励网站分享出更好的教程