Spring中Bean的装配方式

1.基于xml的装配

bean.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 definitions here -->
<bean id="peo" class="cn.qyc.Beanzhuangpei.People">
<property name="name" value="强月城"></property>
<property name="sex" value="男"></property>
<property name="home" ref="homeaddress"></property>
<property name="school" ref="schooladdress"></property>
</bean>
<bean id="homeaddress" class="cn.qyc.Beanzhuangpei.Address">
<property name="address" value="山西省"></property>
<property name="phone" value="17635800128"></property>
</bean>
<bean id="schooladdress" class="cn.qyc.Beanzhuangpei.Address">
<property name="address" value="黑龙江省"></property>
<property name="phone" value="18845045359"></property>
</bean>
</beans>

address

package cn.qyc.Beanzhuangpei;

public class Address {
	private String address;
	private String phone;
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	@Override
	public String toString() {
		return "Address [address=" + address + ", phone=" + phone + "]";
	}
	
}

people

package cn.qyc.Beanzhuangpei;

public class People {
	private String name;
	private String sex;
	private Address home;
	private Address school;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Address getHome() {
		return home;
	}
	public void setHome(Address home) {
		this.home = home;
	}
	public Address getSchool() {
		return school;
	}
	public void setSchool(Address school) {
		this.school = school;
	}
	@Override
	public String toString() {
		return "People [name=" + name + ", sex=" + sex + ", home=" + home + ", school=" + school + "]";
	}
	
}

test

package cn.qyc.Beanzhuangpei;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
	@org.junit.Test
	public void test(){
		String xmlpath = "cn/qyc/Beanzhuangpei/bean.xml";
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
		People people = (People) applicationContext.getBean("peo");
		System.out.println(people.toString());
	}
	
	
}

People [name=强月城, sex=男, home=Address [address=山西省, phone=17635800128],

school=Address [address=黑龙江省, phone=18845045359]]

 

 

2.SpEL

bean.xml

<bean id="peoSpEL" class="cn.qyc.Beanzhuangpei.SpEL">
<property name="name" value="#{peoSpEL.name.toUpperCase()}"></property>
<property name="pi" value="#{T(java.lang.Math).PI}"></property>
</bean>

SpEL

package cn.qyc.Beanzhuangpei;

public class SpEL {
	public String name = "qyc";
	public double pi;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getPi() {
		return pi;
	}
	public void setPi(double pi) {
		this.pi = pi;
	}
	@Override
	public String toString() {
		return "SpEL [name=" + name + ", pi=" + pi + "]";
	}
	
}

test

	public void test(){
		String xmlpath = "cn/qyc/Beanzhuangpei/bean.xml";
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlpath);
		SpEL spEL = (SpEL) applicationContext.getBean("peoSpEL");
		System.out.println(spEL);
		
	}
	

SpEL [name=QYC, pi=3.141592653589793]

3.P命名

配置

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd">

 bean.xml

<bean id="peo" class="cn.qyc.Beanzhuangpei.People" 
		p:name="强月城" 
		p:sex="男" 
		p:home-ref="homeaddress"
		p:school-ref="schooladdress">
</bean>

People [name=强月城, sex=男, home=Address [address=山西省, phone=17635800128],

school=Address [address=黑龙江省, phone=18845045359]]

 剩余代码和第一个一样,运行结果也一样

 

4.集合注入

<!-- 
集合的注入都是给<property>添加子标签 
数组:<array> List:<list> 
Set:<set> 
Map:<map> ,map存放k/v 键值对,使用<entry>描述 <entry key="jack" value="杰克"></entry>
Properties:<props> <prop key="">***</prop> 

普通数据:<value> 
引用数据:<ref>
 -->
<property name="arrayData"> 
<array> 
<value>1</value> 
<value>2</value>
<value>3</value> 
<value>4</value> 
</array> 
</property>
<property name="mapData"> 
<map> 
<entry key="jack" value="杰克"></entry> 

<entry> 
<key><value>rose</value></key> 
<value>肉丝</value> 
</entry> 

</map> 
</property>
<property name="propsData"> 
<props> 
<prop key="高富帅">嫐</prop> 
<prop key="白富美">嬲</prop> 
<prop key="男屌丝">挊</prop> 
</props> 
</property>

properties  ::

  1. https://www.cnblogs.com/xudong-bupt/p/3758136.html
  2. https://blog.csdn.net/qq_41961660/article/details/80697823

 

 

5.基于注解

1. @Component取代<bean class="">

@Component("id") 取代 <bean id="" class="">

 

2.web开发,提供3个@Component注解衍生注解(功能一样)取代<bean class="">

@Repository :dao层

@Service:service层

@Controller:web层

 

3.依赖注入,给私有字段设置,也可以给setter方法设置

普通值:@Value("")

引用值:

方式1:按照【类型】注入

@Autowired

方式2:按照【名称】注入1

@Autowired

@Qualifier("名称")

方式3:按照【名称】注入2

@Resource("名称")

 

4.生命周期

初始化:@PostConstruct

销毁:@PreDestroy

 

5.作用域

@Scope("prototype") 多例

配置

<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 组件扫描,扫描含有注解的类 --> 
<context:component-scan base-package="com.itheima.g_annotation.a_ioc"></context:component-scan> 
</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值