目录
一、注入外部Bean与内部Bean
public class OrderDao {
private static final Logger logger = LoggerFactory.getLogger(UserDao.class);
public void insert(){
logger.info("订单正在生成...");
}
}
public class OrderService {
private OrderDao orderDao;
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
/**
* 生产订单的业务方法
*/
public void save(){
orderDao.insert();
}
}
<?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-->
<bean id="orderBean" class="com.cjc.spring6.dao.OrderDao"></bean>
<bean id="orderServiceBean" class="com.cjc.spring6.service.OrderService">
<!--使用ref引入,这里就是实现注入外部Bean-->
<property name="orderDao" ref="orderBean"/>
</bean>
<bean id="orderServiceBean1" class="com.cjc.spring6.service.OrderService">
<property name="orderDao">
<!--在property标签中嵌套bean标签,就是内部Bean-->
<bean class="com.cjc.spring6.dao.OrderDao"></bean>
</property>
</bean>
</beans>
@Test
public void getOrder(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring02.xml");
OrderService orderService = applicationContext.getBean("orderServiceBean", OrderService.class);
orderService.save();
}
打印结果:
2022-11-13 23:30:38.767 [main] INFO com.cjc.spring6.dao.UserDao - 订单正在生成...
二、简单类型注入
引入依赖
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.10</version>
</dependency>
@Data
@ToString
public class User {
private String username;
private String password;
private int age;
}
配置文件:
<!--注入简单类型-->
<bean id="userBean" class="com.cjc.spring6.bean.User">
<!--给简单属性赋值使用value属性-->
<property name="username" value="老王"/>
<property name="password" value="123456"/>
<property name="age" value="18"/>
</bean>
测试类:
@Test
public void getUserType(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring02.xml");
User user = applicationContext.getBean("userBean", User.class);
System.out.println(user);
}
测试结果:
简单类型的注入应用于数据库数据源管理:
@Data
@ToString
public class MyDataSource implements DataSource {
private String driver;
private String url;
private String name;
private String 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;
}
}
spring的配置文件:
<bean id="myDataSource" class="com.cjc.spring6.jdbc.MyDataSource">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/spring6"/>
<property name="name" value="root"/>
<property name="password" value="123456"/>
</bean>
测试代码:
@Test
public void getData(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring02.xml");
MyDataSource dataSource = applicationContext.getBean("myDataSource", MyDataSource.class);
System.out.println(dataSource);
}
测试结果:
三、级联属性赋值
@Data
@ToString
public class Clazz {
private String name;
}
@Data
@ToString
public class Student {
private String name;
private Clazz clazz;
}
spring配置文件:
<?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">
<!--使用级联属性赋值需要注意:
1、配置的顺序不能颠倒
2、clazz属性必须提供getter方法-->
<bean id="clazzBean" class="com.cjc.spring6.bean.Clazz"></bean>
<bean id="student" class="com.cjc.spring6.bean.Student">
<property name="name" value="小丽"/>
<property name="clazz" ref="clazzBean"/>
<!--级联属性赋值-->
<property name="clazz.name" value="学期班"/>
</bean>
</beans>
测试代码:
@Test
public void getStudent(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring3.xml");
Clazz clazz = applicationContext.getBean("clazzBean", Clazz.class);
Student student = applicationContext.getBean("student", Student.class);
System.out.println(clazz);
System.out.println(student);
}
测试结果:
四、注入数组
@Data
@ToString
public class Cat {
private String name;
}
@Data
@ToString
public class ArrayLove {
private String[] love;
private Cat[] cats;
}
spring配置文件
<?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="cat" class="com.cjc.spring6.bean.Cat">
<property name="name" value="小白"/>
</bean>
<bean id="love" class="com.cjc.spring6.bean.ArrayLove">
<property name="love">
<!--数组的属性为简单类型-->
<array>
<value>篮球</value>
<value>羽毛球</value>
<value>电影</value>
</array>
</property>
<!--数组的属性为引用类型-->
<property name="cats">
<array>
<ref bean="cat"/>
</array>
</property>
</bean>
</beans>
测试代码:
@Test
public void getLove(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("array.xml");
ArrayLove love = applicationContext.getBean("love", ArrayLove.class);
System.out.println(love);
}
测试结果:
五、List与Set注入
@Data
@ToString
public class People {
private List<String> skill;
private Set<String> province;
}
spring的配置文件
<?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="people" class="com.cjc.spring6.bean.People">
<property name="skill">
<list>
<value>洗衣</value>
<value>做饭</value>
<value>跳舞</value>
</list>
</property>
<property name="province">
<set>
<value>四川</value>
<value>上海</value>
</set>
</property>
</bean>
</beans>
测试代码:
@Test
public void getPeople(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("collection.xml");
People people = applicationContext.getBean("people", People.class);
System.out.println(people);
}
People(skill=[洗衣, 做饭, 跳舞], province=[四川, 上海])
六、Map和Properties注入
@Data
@ToString
public class MapTest {
//注入map集合
private Map<Integer,String> phones;
//注入属性类
//Properties的父类是Hashtable Hashtable实现了Map接口,本质上相当于Map集合
//键值的类型都是String类型
private Properties properties;
}
<?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="mapBean" class="com.cjc.spring6.bean.MapTest">
<property name="phones">
<map>
<entry key="1" value="123456789"/>
<entry key="2" value="12345678"/>
<entry key="3" value="123456780"/>
</map>
</property>
<property name="properties">
<props>
<prop key="name">张三</prop>
<prop key="password">123456</prop>
</props>
</property>
</bean>
</beans>
@Test
public void getMap(){
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("maptest.xml");
MapTest mapBean = applicationContext.getBean("mapBean", MapTest.class);
System.out.println(mapBean);
}
MapTest(phones={1=123456789, 2=12345678, 3=123456780}, properties={password=123456, name=张三})