Spring学习(二)

Spring依赖注入

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.xiaoguan.bean;

public class Cat {
private String name;
private int ager;

@Override
public String toString() {
return "Cat{" +
"name='" + name + '\'' +
", ager=" + ager +
'}';
}

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

public void setAger(int ager) {
this.ager = ager;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.xiaoguan.bean;

public class Clazz {
private String name;

@Override
public String toString() {
return "Clazz{" +
"name='" + name + '\'' +
'}';
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.xiaoguan.bean;

import java.util.Date;

public class Doge {
private String name;
private int age;
private Date birth;

@Override
public String toString() {
return "Doge{" +
"name='" + name + '\'' +
", age=" + age +
", birth=" + birth +
'}';
}

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

public void setAge(int age) {
this.age = age;
}

public void setBirth(Date birth) {
this.birth = birth;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.xiaoguan.bean;

public class MathBean {
private String result;

@Override
public String toString() {
return "MathBean{" +
"result='" + result + '\'' +
'}';
}

public void setResult(String result) {
this.result = result;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.xiaoguan.bean;

public class People {
private String name;
private String age;
private String sex;

@Override
public String toString() {
return "People{" +
"name='" + name + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
'}';
}

public People(String name, String age, String sex) {
this.name = name;
this.age = age;
this.sex = sex;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.xiaoguan.bean;

import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

public class Person {
private List<String> names;
private Map<Integer,String> phones;
private Properties properties;

@Override
public String toString() {
return "Person{" +
"names=" + names +
", phones=" + phones +
", properties=" + properties +
", addrs=" + addrs +
'}';
}

public void setProperties(Properties properties) {
this.properties = properties;
}

public void setPhones(Map<Integer, String> phones) {
this.phones = phones;
}

public void setAddrs(Set<String> addrs) {
this.addrs = addrs;
}

private Set<String> addrs;

public void setNames(List<String> names) {
this.names = names;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.xiaoguan.bean;

import java.util.Arrays;

public class QianDaYe {
private String[] habits;

public void setWomen(Women[] women) {
this.women = women;
}

private Women[] women;

@Override
public String toString() {
return "QianDaYe{" +
"habits=" + Arrays.toString(habits) +
", women=" + Arrays.toString(women) +
'}';
}


public void setHabits(String[] habits) {
this.habits = habits;
}
}

1
2
3
4
5
6
package com.xiaoguan.bean;

public enum Seanson {
SPRING,SUMMER,AUTUMN,WINTER
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package com.xiaoguan.bean;

import java.util.Date;

public class SimpleDITest {
/*简单数据类型
return (Void.class != type && void.class != type &&
(isPrimitiveOrWrapper(type) ||
Enum.class.isAssignableFrom(type) ||
CharSequence.class.isAssignableFrom(type) ||
Number.class.isAssignableFrom(type) ||
Date.class.isAssignableFrom(type) ||
Temporal.class.isAssignableFrom(type) ||
ZoneId.class.isAssignableFrom(type) ||
TimeZone.class.isAssignableFrom(type) ||
File.class.isAssignableFrom(type) ||
Path.class.isAssignableFrom(type) ||
Charset.class.isAssignableFrom(type) ||
Currency.class.isAssignableFrom(type) ||
InetAddress.class.isAssignableFrom(type) ||
URI.class == type ||
URL.class == type ||
UUID.class == type ||
Locale.class == type ||
Pattern.class == type ||
Class.class == type));
*/
private int age;
private Integer age2;
private boolean flag;
private Boolean flag2;
private char c;
private Character c2;
private Seanson seanson;
private String userName;
private Class clazz;
private Date date;

@Override
public String toString() {
return "SimpleDITest{" +
"age=" + age +
", age2=" + age2 +
", flag=" + flag +
", flag2=" + flag2 +
", c=" + c +
", c2=" + c2 +
", seanson=" + seanson +
", userName='" + userName + '\'' +
", clazz=" + clazz +
", date=" + date +
'}';
}

public void setDate(Date date) {
this.date = date;
}

public void setAge(int age) {
this.age = age;
}

public void setAge2(Integer age2) {
this.age2 = age2;
}

public void setFlag(boolean flag) {
this.flag = flag;
}

public void setFlag2(Boolean flag2) {
this.flag2 = flag2;
}

public void setC(char c) {
this.c = c;
}

public void setC2(Character c2) {
this.c2 = c2;
}

public void setSeanson(Seanson seanson) {
this.seanson = seanson;
}

public void setUserName(String userName) {
this.userName = userName;
}

public void setClazz(Class clazz) {
this.clazz = clazz;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.xiaoguan.bean;

public class Student {
private String name;
private Clazz clazz;

@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", clazz=" + clazz +
'}';
}

public void setClazz(Clazz clazz) {
this.clazz = clazz;
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package com.xiaoguan.bean;

public class User {
private String username;
private String password;
private int age;

@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}

public void setUsername(String username) {
this.username = username;
}

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

public void setAge(int age) {
this.age = age;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.xiaoguan.bean;

public class Women {
private String name;

@Override
public String toString() {
return "Women{" +
"name='" + name + '\'' +
'}';
}

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

1
2
3
4
5
6
7
8
9
10
11
12
package com.xiaoguan.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class OrderDao {
private static final Logger logger= LoggerFactory.getLogger(OrderDao.class);
public void insert(){
logger.info("订单生成了!");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
package com.xiaoguan.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class UserDao {
private Logger logger =LoggerFactory.getLogger(UserDao.class);
public void insert(){
// System.out.println("数据库正在插入信息");
logger.info("数据库正在插入信息");
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.xiaoguan.dao;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VipDao {
private Logger logger = LoggerFactory.getLogger(VipDao.class);
public void insert(){
// System.out.println("数据库正在插入信息");
logger.info("Vip数据库正在插入信息");
}

}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.xiaoguan.jdbc;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

public class MyDataSource1 implements DataSource {
private Properties properties;

@Override
public String toString() {
return "MyDataSource1{" +
"properties=" + properties +
'}';
}

public void setProperties(Properties properties) {
this.properties = properties;
}

@Override
public Connection getConnection() throws SQLException {
return null;
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {

}

@Override
public void setLoginTimeout(int seconds) throws SQLException {

}

@Override
public int getLoginTimeout() throws SQLException {
return 0;
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package com.xiaoguan.jdbc;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.Properties;
import java.util.logging.Logger;

public class MyDataSource2 implements DataSource {
// private String driver;
// private String url;
// private String username;
// private String password;
private Properties properties;

@Override
public String toString() {
return "MyDataSource1{" +
"properties=" + properties +
'}';
}

public void setProperties(Properties properties) {
this.properties = properties;
}

@Override
public Connection getConnection() throws SQLException {
return null;
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {

}

@Override
public void setLoginTimeout(int seconds) throws SQLException {

}

@Override
public int getLoginTimeout() throws SQLException {
return 0;
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.xiaoguan.jdbc;

import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;

public class MyDateSource implements DataSource {
private String driver;
private String url;
private String userName;
private String password;

@Override
public String toString() {
return "MyDateSource{" +
"driver='" + driver + '\'' +
", url='" + url + '\'' +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
'}';
}

public void setDriver(String driver) {
this.driver = driver;
}

public void setUrl(String url) {
this.url = url;
}

public void setUserName(String userName) {
this.userName = userName;
}

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

@Override
public Connection getConnection() throws SQLException {
return null;
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
return null;
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return null;
}

@Override
public void setLogWriter(PrintWriter out) throws SQLException {

}

@Override
public void setLoginTimeout(int seconds) throws SQLException {

}

@Override
public int getLoginTimeout() throws SQLException {
return 0;
}

@Override
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
return null;
}

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
return null;
}

@Override
public boolean isWrapperFor(Class<?> iface) throws SQLException {
return false;
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.xiaoguan.service;

import com.xiaoguan.dao.UserDao;
import com.xiaoguan.dao.VipDao;

public class CustomService {
private UserDao userDao;
private VipDao vipDao;


public CustomService(UserDao userDao, VipDao vipDao) {
this.userDao = userDao;
this.vipDao = vipDao;
}

public void setVipDao(VipDao vipDao) {
this.vipDao = vipDao;
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public CustomService() {
}

public void save(){
userDao.insert();
vipDao.insert();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
package com.xiaoguan.service;

import com.xiaoguan.dao.OrderDao;

public class OrderService {
private OrderDao orderDao;

public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}

public void generate(){
orderDao.insert();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.xiaoguan.service;

import com.xiaoguan.dao.UserDao;
import com.xiaoguan.dao.VipDao;

public class UserService {
private UserDao userDao;
private VipDao vipDao;

public void setVipDao(VipDao vipDao) {
this.vipDao = vipDao;
}

public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}

public void saveUser(){
userDao.insert();
vipDao.insert();
}
}

1
2
3
4
5
6
7
8
9
10
11
12
<?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="studentBean" class="com.xiaoguan.bean.Student">
<property name="name" value="张三"/>
<property name="clazz" ref="clazzBean"/>
</bean>
<bean id="clazzBean" class="com.xiaoguan.bean.Clazz">
<property name="name" value="高三一班"/>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
<?xml version="1.0" encoding="UTF-8"?>

<configuration>

<loggers>
<!--
level指定日志级别,从低到高的优先级:
ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF
-->
<root level="INFO">
<appender-ref ref="spring6log"/>
</root>
</loggers>

<appenders>
<!--输出日志信息到控制台-->
<console name="spring6log" target="SYSTEM_OUT">
<!--控制日志输出的格式-->
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-3level %logger{1024} - %msg%n"/>
</console>
</appenders>

</configuration>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?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="orderDaoBean" class="com.xiaoguan.dao.OrderDao"/>
<bean id="orderService" class="com.xiaoguan.service.OrderService">
<property name="orderDao" ref="orderDaoBean"></property>
</bean>
<bean id="orderService2" class="com.xiaoguan.service.OrderService">
<property name="orderDao">
<bean class="com.xiaoguan.dao.OrderDao"></bean>
</property>
</bean>
<bean id="userBean" class="com.xiaoguan.bean.User">
<property name="username" value="张三"/>
<property name="age" value="23"/>
<property name="password" value="123"/>
</bean>
<bean id="SimpleDIBean" class="com.xiaoguan.bean.SimpleDITest">
<property name="age" value="20"/>
<property name="age2" value="20"/>
<property name="userName" value="张三"/>
<property name="flag" value="false"/>
<property name="seanson" value="SPRING"/>
<property name="flag2" value="true"/>
<property name="c" value="男"/>
<property name="c2" value="女"/>
<property name="clazz" value="java.lang.String"/>
<property name="date" value="Wed Jul 19 20:05:41 CST 2023"/>
</bean>
<bean id="myDatasource" class="com.xiaoguan.jdbc.MyDateSource">
<property name="userName" value="1"/>
<property name="password" value="2"/>
<property name="driver" value="3"/>
<property name="url" value="4"/>
</bean>
<bean id="catBean" class="com.xiaoguan.bean.Cat">
<!-- 不给属性注入默认值就是null,写字符串null是字符串可以转大写方法,而不给属性则不能-->
<!-- <property name="name" value="tom"/>-->
<property name="name" value=""/>

<property name="ager" value="3"/>
</bean>
<bean id="mathBean" class="com.xiaoguan.bean.MathBean">
<!-- <property name="result" value="2&lt;3"/>-->
<!-- 特殊字符 转义字符-->
<!-- > &gt;-->
<!-- < &lt;-->
<!-- ' &apos;-->
<!-- " &quot;-->
<!-- & &amp;-->
<property name="result">
<value><![CDATA[2<3]]></value>
<!-- 这种方法只能value标签-->

</property>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?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="userDaoBean" class="com.xiaoguan.dao.UserDao"/>
<bean id="vipBean" class="com.xiaoguan.dao.VipDao"/>
<bean id="userServiceBean" class="com.xiaoguan.service.UserService">
<property name="userDao" ref="userDaoBean"/>
<property name="vipDao" ref="vipBean"/>
</bean>
<bean id="customService" class="com.xiaoguan.service.CustomService">
<!-- o和1也可以用构造方法的参数名字代替,不指定index也可以,Spring能够自己参数匹配-->
<constructor-arg index="0" ref="userDaoBean"/>
<constructor-arg index="1 " ref="vipBean"/>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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="orderDao" class="com.xiaoguan.dao.OrderDao"></bean>-->
<!-- <bean id="orderService" class="com.xiaoguan.service.OrderService">-->
<!-- <property name="orderDao" ref="orderDao"/>-->
<!-- </bean>-->
<!-- 根据名字自动装配-->
<bean id="orderDao" class="com.xiaoguan.dao.OrderDao"></bean>
<bean id="orderService" class="com.xiaoguan.service.OrderService" autowire="byName">
</bean>
<bean class="com.xiaoguan.dao.VipDao"></bean>
<bean class="com.xiaoguan.dao.UserDao"></bean>
<bean id="cs" class="com.xiaoguan.service.CustomService" autowire="byType"></bean>
<!-- 这种方式要set注入,根据无参数构建对象-->
</beans>
1
2
3
4
5
6
7
<?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: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 id="peopleBean" class="com.xiaoguan.bean.People" c:name="1" c:age="2" c:sex="3" />
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?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 class="com.xiaoguan.bean.Person" id="person">
<property name="names">
<list>
<value>张三</value>
<value>李四</value>
<value>王五</value>
<value>赵六</value>
<value>赵六</value>
<value>赵六</value>
</list>
</property>
<property name="addrs">
<!-- set重复去掉-->
<set>
<value>北京大兴区</value>
<value>北京海淀区</value>
<value>北京朝阳区</value>
<value>北京大兴区</value>
</set>
</property>
<property name="phones">
<map>
<entry key="1" value="110"></entry>
<entry key="2" value="120"></entry>
<entry key="3" value="119"></entry>
<!-- 如果不是简单类型就用key-ref和value-ref-->
</map>
</property>
<property name="properties">
<props>
<prop key="driver">com.xiaoguan</prop>
<prop key="driver">com.xiaoguan</prop>
<prop key="driver">com.xiaoguan</prop>
<!-- 本质是map集合但是注入方法特殊-->
</props>
</property>
</bean>
</beans>
1
2
3
4
5
6
7
8
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="dogBean" class="com.xiaoguan.bean.Doge" p:name="花花" p:age="3" p:birth-ref="birthBean"/>
<bean id="birthBean" class="java.util.Date" />
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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: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:property-placeholder location="jdbc.properties"/>
<bean id="ds" class="com.xiaoguan.jdbc.MyDateSource">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="userName" value="${jdbc.username}"/>
<property name="password" value="${password}"/>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?xml version="1.0" encoding="UTF-8"?>
<!--引入util命名空间-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd ">
<util:properties id="prop">
<prop key="driver">com.mysql.cj.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/spring6</prop>
<prop key="uesrname">root</prop>
<prop key="password">root</prop>
</util:properties>
<bean id="ds1" class="com.xiaoguan.jdbc.MyDataSource1">
<property name="properties" ref="prop"/>
</bean>
<bean id="ds2" class="com.xiaoguan.jdbc.MyDataSource2">
<property name="properties" ref="prop"/>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?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="w1" class="com.xiaoguan.bean.Women">
<property name="name" value="小花"/>
</bean>
<bean id="w2" class="com.xiaoguan.bean.Women">
<property name="name" value="小红"/>
</bean>
<bean id="w3" class="com.xiaoguan.bean.Women">
<property name="name" value="小明"/>
</bean>
<bean class="com.xiaoguan.bean.QianDaYe" id="qianDaYe">
<property name="habits">
<array>
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</array>
</property>
<property name="women">
<array>
<ref bean="w1"/>
<ref bean="w2"/>
<ref bean="w3"/>
</array>
</property>
</bean>
</beans>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.xiaoguan.test;
import com.xiaoguan.bean.*;
import com.xiaoguan.jdbc.MyDataSource1;
import com.xiaoguan.jdbc.MyDataSource2;
import com.xiaoguan.jdbc.MyDateSource;
import com.xiaoguan.service.CustomService;
import com.xiaoguan.service.OrderService;
import com.xiaoguan.service.UserService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.Date;

public class SpringDITest {
@Test
public void testJDBCProperties() {
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-properties.xml");
MyDateSource ds = applicationContext.getBean("ds", MyDateSource.class);
System.out.println(ds);
}
@Test
public void testAutowire2(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-autowire.xml");
CustomService bean = applicationContext.getBean("cs", CustomService.class);
bean.save();
}
@Test
public void testAutowire(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-autowire.xml");
OrderService orderService = applicationContext.getBean("orderService", OrderService.class);
orderService.generate();
}
@Test
public void testUtil(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-util.xml");
MyDataSource1 ds1 = applicationContext.getBean("ds1", MyDataSource1.class);
System.out.println(ds1);
MyDataSource2 ds2 = applicationContext.getBean("ds2", MyDataSource2.class);
System.out.println(ds2);
}
@Test
public void testC(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-c.xml");
People peoplec = applicationContext.getBean("peopleBean", People.class);
System.out.println(peoplec);

}
@Test
public void testP(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-p.xml");
Doge dogBean = applicationContext.getBean("dogBean", Doge.class);
System.out.println(dogBean);
}
@Test
public void testMathResult(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("set-di.xml");
MathBean mathBean = applicationContext.getBean("mathBean", MathBean.class);
System.out.println(mathBean);
}
@Test
public void testCarBean(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("set-di.xml");
Cat catBean = applicationContext.getBean("catBean", Cat.class);
System.out.println(catBean);
}
@Test
public void testCollection(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring-collection.xml");
Person person = applicationContext.getBean("person", Person.class);
System.out.println(person);
}
@Test
public void testArray(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("springArray.xml");
QianDaYe qianDaYe = applicationContext.getBean("qianDaYe", QianDaYe.class);
System.out.println(qianDaYe);
}
@Test
public void testCascade(){
ApplicationContext applicationContext= new ClassPathXmlApplicationContext("cascade.xml");
Student studentBean = applicationContext.getBean("studentBean", Student.class);
Clazz clazzBean = applicationContext.getBean("clazzBean", Clazz.class);
System.out.println(studentBean);
System.out.println(clazzBean);
}
@Test
public void testSetDi4() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
MyDateSource myDatasource = applicationContext.getBean("myDatasource", MyDateSource.class);
System.out.println(myDatasource);
}
@Test
public void testSetDi3() {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("set-di.xml");
SimpleDITest bean = applicationContext.getBean("SimpleDIBean", SimpleDITest.class);
System.out.println(bean);
}
@Test
public void testSetDi2(){

ApplicationContext applicationContext=new ClassPathXmlApplicationContext("set-di.xml");
User userBean = applicationContext.getBean("userBean", User.class);
System.out.println(userBean);
}
@Test
public void testOrderSet(){
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("set-di.xml");
OrderService orderService = applicationContext.getBean("orderService2", OrderService.class);
orderService.generate();
}
@Test
public void testCustomService(){
ApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.xml");
CustomService bean = classPathXmlApplicationContext.getBean("customService",CustomService.class);
bean.save();
}

@Test
public void testSpringDi(){
ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring.xml");
UserService userServiceBean = classPathXmlApplicationContext.getBean("userServiceBean", UserService.class);
userServiceBean.saveUser();
}
}