1.导入maven依赖搭建spring环境
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.21</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
</dependencies>
2.实体类
User类
package com.hwy.beans;
public class User {
private Integer id;
private String username;
private String realname;
public User(){
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getRealname() {
return realname;
}
public void setRealname(String realname) {
this.realname = realname;
}
public User(Integer id, String username, String realname) {
this.id = id;
this.username = username;
this.realname = realname;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", realname='" + realname + '\'' +
'}';
}
}
Person类
package com.hwy.beans;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import java.util.Date;
import java.util.List;
import java.util.Map;
public class Person implements InitializingBean, DisposableBean {
private Integer id;
private String name;
private String gender;
private Date birthday;
private List<String> hobbies;
private Map<Integer,String> course;
private Wife wife;
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 getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public List<String> getHobbies() {
return hobbies;
}
public void setHobbies(List<String> hobbies) {
this.hobbies = hobbies;
}
public Map<Integer, String> getCourse() {
return course;
}
public void setCourse(Map<Integer, String> course) {
this.course = course;
}
public Wife getWife() {
return wife;
}
public void setWife(Wife wife) {
this.wife = wife;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", gender='" + gender + '\'' +
", birthday=" + birthday +
", hobbies=" + hobbies +
", course=" + course +
", wife=" + wife +
'}';
}
public Person(){
System.out.println("Person已加载");
}
public Person(Wife wife3) {
this.wife = wife3;
}
public static Person createPersonFactory(){
Child child = new Child();
child.setName("儿子");
return child;
}
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("实例化bean1");
}
@Override
public void destroy() throws Exception {
System.out.println("销毁bean1");
}
public void initByConfig() throws Exception{
System.out.println("实例化bean2");
}
public void destroyByConfig() throws Exception{
System.out.println("销毁bean2");
}
}
Child类
package com.hwy.beans;
public class Child extends Person {
}
Wife类
package com.hwy.beans;
public class Wife {
private Integer age;
private String name;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Wife(Integer age, String name) {
this.age = age;
this.name = name;
}
public Wife(){
}
@Override
public String toString() {
return "Wife{" +
"age=" + age +
", name='" + name + '\'' +
'}';
}
}
PersonFactory类
package com.hwy.beans;
import java.util.Date;
public class PersonFactory {
public Person createPersonFactoryMethod() {
Child child = new Child();
child.setName("儿子");
return child;
}
public static Date getNowDate(){
return new Date();
}
}
3.SpringIOC基本配置
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean class="com.hwy.beans.User" id="user" name="user2 user3">
<description>描述bean是干嘛的</description>
</bean>
<alias name="user" alias="userbean"></alias>
<!--使用import可以导入其他spring的xml配置文件-->
<!--<import resource="spring-ioc.xml"></import>-->
<!--**************依赖注入Begin**************-->
<!--基于setter方法的依赖注入-->
<!--注意name对应setXX中的XX setId->id-->
<bean class="com.hwy.beans.User" id="user4">
<property name="id" value="1"></property>
<property name="username" value="何婉滢"></property>
<property name="realname" value="加油啊"></property>
</bean>
<!--基于构造方法的依赖注入-->
<!--1.name可省略只有value注意顺序 2.可用下标index从0开始 3.可使type指定类型-->
<bean class="com.hwy.beans.User" id="user5">
<constructor-arg name="id" value="1"></constructor-arg>
<constructor-arg name="username" value="何婉滢"></constructor-arg>
<constructor-arg name="realname" value="会成功的"></constructor-arg>
</bean>
<!--复杂数据类型的依赖注入-->
<bean class="com.hwy.beans.Person" id="person">
<property name="id" value="1"></property>
<property name="name" ><null></null></property>
<property name="gender" value=""></property>
<!--引用外部bean
<property name="wife" ref="wife"></property>-->
<!--引用内部bean-->
<property name="wife">
<bean class="com.hwy.beans.Wife">
<property name="age" value="18"></property>
<property name="name" value="hwy"></property>
</bean>
</property>
<!--泛型是基本数据类型用value 是对象使用bean-->
<property name="hobbies">
<list>
<value>何</value>
<value>何何</value>
</list>
</property>
<!--泛型是基本数据类型用key-value 是对象用key-value-ref-->
<property name="course" >
<map>
<entry key="1" value="冲"></entry>
<entry key="2" value="啊"></entry>
</map>
</property>
</bean>
<!--p命名空间简化基于setter属性注入xml配置 集合还是要用property-->
<bean class="com.hwy.beans.Wife" id="wife" p:age="22" p:name="何婉滢"></bean>
<bean class="com.hwy.beans.Person" id="person2" p:wife-ref="wife2"></bean>
<!--c命名空间简化基于构造函数注入xml配置-->
<bean class="com.hwy.beans.Wife" id="wife2" c:age="20" c:name="婉婉"></bean>
<!--**************依赖注入End**************-->
</beans>
4.基本配置测试类
package com.hwy.beans.tests;
import com.hwy.beans.Person;
import com.hwy.beans.User;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IocTest {
ApplicationContext ioc;
@Before
public void before() {
ioc=new ClassPathXmlApplicationContext("spring-ioc.xml");
}
@Test
public void test01() {
User user = (User)ioc.getBean("user");
System.out.println(user);
}
/*
* 通过别名获取bean,原名也能用
* */
@Test
public void test02(){
User user = (User)ioc.getBean("user3");
System.out.println(user);
}
/*
* 基于setter方法的依赖注入
* */
@Test
public void test03(){
User user = (User)ioc.getBean("user4");
System.out.println(user);
}
/*
* 基于构造方法的依赖注入
* */
@Test
public void test04(){
User user = (User)ioc.getBean("user5");
System.out.println(user);
}
/*
* 复杂数据类型的依赖注入
* */
@Test
public void test05(){
Person person = (Person)ioc.getBean("person");
System.out.println(person);
}
/*
* p命名空间简化基于setter属性
* */
@Test
public void test06(){
Person person = (Person)ioc.getBean("person2");
System.out.println(person);
}
/*
* c命名空间简化基于构造函数注入xml
* */
@Test
public void test07(){
Person person = (Person)ioc.getBean("person2");
System.out.println(person);
}
}
5.SpringIOC高级配置
<?xml version="1.0" encoding="UTF-8"?>
<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"
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
https://www.springframework.org/schema/context/spring-context.xsd">
<!--控制加载顺序
<bean class="com.hwy.beans.Person" id="person" depends-on="wife"></bean>
<bean class="com.hwy.beans.Wife" id="wife"></bean>-->
<!--懒加载
<bean class="com.hwy.beans.Wife" id="wife" lazy-init="true"></bean>-->
<!--作用域 singleton:同一id只创建一个bean prototype:每一次使用都创建bean
<bean class="com.hwy.beans.Person" id="person" scope="prototype"></bean>-->
<!--使用静态工厂实例化bean
<bean class="com.hwy.beans.Person" id="person" factory-method="createPersonFactory"></bean>-->
<!--使用实例工厂实例化bean
<bean class="com.hwy.beans.PersonFactory" id="personFactory"></bean>
<bean class="com.hwy.beans.Person" id="person"
factory-bean="personFactory"
factory-method="createPersonFactoryMethod">
</bean>-->
<!--自动注入
byType:匹配到多个类型会报错
byName:根据set方法名字匹配
constructor:根据构造方法参数名匹配 名字匹配不到根据类型匹配
出现多个类型会报错可设置:1.primary主要bean
2.设置不需要自动注入的bean autowire-candidate="false"
<bean class="com.hwy.beans.Person" id="person" autowire="constructor"></bean>
<bean class="com.hwy.beans.Wife" id="wife" p:age="00" p:name="可爱" autowire-candidate="false"></bean>
<bean class="com.hwy.beans.Wife" id="wife2" p:age="66" p:name="好的"></bean>-->
<!--bean生命周期回调-->
<bean class="com.hwy.beans.Person" id="person"
init-method="initByConfig"
destroy-method="destroyByConfig"></bean>
<!--配置第三方bean-->
<bean class="com.alibaba.druid.pool.DruidDataSource" id="dataSource">
<property name="username" value="${mysql.username}"></property>
<property name="password" value="${mysql.password}"></property>
<property name="url" value="${mysql.url}"></property>
<property name="driverClassName" value="${mysql.driverName}"></property>
</bean>
<!--引入外部属性资源文件-->
<context:property-placeholder location="db.properties"></context:property-placeholder>
<!--运算表达式-->
<bean class="com.hwy.beans.Person" id="person2">
<property name="id" value="#{1+2}"></property>
<property name="wife" value="#{wife}"></property>
<property name="name" value="#{wife.name}"></property>
<property name="birthday" value="#{T(com.hwy.beans.PersonFactory).getNowDat e()}"></property>
</bean>
<bean class="com.hwy.beans.Wife" id="wife" p:age="22" p:name="婉儿"></bean>
</beans>
db.properties
mysql.username=root
mysql.password=HWY666888
mysql.url=jdbc:mysql://localhost:3306/test
mysql.driverName=com.mysql.jdbc.Driver
6.高级配置测试类
package com.hwy.beans.tests;
import com.alibaba.druid.pool.DruidDataSource;
import com.hwy.beans.Person;
import com.hwy.beans.Wife;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class IocHighTest {
ApplicationContext ioc;
@Before
public void before() {
ioc=new ClassPathXmlApplicationContext("spring-ioc-high.xml");
}
@Test
public void test01() {
Wife wife = (Wife)ioc.getBean("wife");
System.out.println(wife);
}
/*
* 作用域
* */
@Test
public void test02() {
System.out.println("Spring已加载");
Person person = (Person)ioc.getBean("person",Person.class);
Person person2 = (Person)ioc.getBean("person",Person.class);
System.out.println(person);
}
/*
* 使用静态工厂实例化bean
* */
@Test
public void test03() {
Person person = (Person)ioc.getBean("person",Person.class);
System.out.println(person);
}
/*
* 使用实例工厂实例化bean
* */
@Test
public void test04() {
Person person = (Person)ioc.getBean("person",Person.class);
System.out.println(person);
}
/*
* 自动注入
* */
@Test
public void test05() {
Person person = (Person)ioc.getBean("person",Person.class);
System.out.println(person);
}
/*
* bean生命周期回调:
* 1.实现接口InitializingBean, DisposableBean 重写方法
* 2.基于xml配置
* */
@Test
public void test06() {
Person person = (Person)ioc.getBean("person",Person.class);
System.out.println(person);
}
/*
* 引入外部属性资源文件
* */
@Test
public void test07() {
DruidDataSource datasource = ioc.getBean("dataSource", DruidDataSource.class);
System.out.println(datasource);
}
/*
* SpEL
* */
@Test
public void test08() {
Person person = (Person)ioc.getBean("person2",Person.class);
System.out.println(person);
}
}