快速入门:
pom文件添加依赖jar包:
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-core
</artifactId>
<version>5.2.7.RELEASE
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-context
</artifactId>
<version>5.2.7.RELEASE
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-beans
</artifactId>
<version>5.2.7.RELEASE
</version>
</dependency>
<dependency>
<groupId>org.springframework
</groupId>
<artifactId>spring-expression
</artifactId>
<version>5.2.7.RELEASE
</version>
</dependency>
<dependency>
<groupId>commons-logging
</groupId>
<artifactId>commons-logging
</artifactId>
<version>1.2
</version>
</dependency>
写一个UserService接口
public interface UserService {
public void add();
}
写一个类实现UserService接口
public class UserServiceImpl implements UserService {
public void add() {
System
.out
.println("增加用户。。。。。");
}
}
beans文件
<?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="userService" class="com.hetl.day01.impl.UserServiceImpl"></bean>
</beans>
测试
@Test
public void test01(){
ApplicationContext c
= new ClassPathXmlApplicationContext("beans.xml");
UserService u1
= (UserService
) c
.getBean("userService");
u1
.add();
UserService u2
= (UserService
) c
.getBean("userService");
u2
.add();
System
.out
.println(u1
);
System
.out
.println(u2
);
}
输出:
增加用户。。。。。
增加用户。。。。。
com
.hetl
.day01
.impl
.UserServiceImpl
@58c1670b
com
.hetl
.day01
.impl
.UserServiceImpl
@58c1670b
u1与u2为同一个值。
依赖注入:
为UserServiceImpl配置get、set方法
public class UserServiceImpl implements UserService {
private String name
;
public String
getName() {
return name
;
}
public void setName(String name
) {
this.name
= name
;
}
public void add() {
System
.out
.println("增加用户。。。。。"+name
);
}
}
在beans.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 id="userService" class="com.hetl.day01.impl.UserServiceImpl">
<property name="name" value="李四"/>
</bean>
</beans>
测试输出
增加用户。。。。。李四
增加用户。。。。。李四
spring容器加载beans.xml的三种方式
第一种 ClassPathXmlApplicationContext(最常用) 第二种 文件系统路径获取配置文件 第三种 使用BeanFactory
@Test
public void test02(){
ApplicationContext c
= new ClassPathXmlApplicationContext("beans.xml");
UserService u
= (UserService
) c
.getBean("userService");
u
.add();
ApplicationContext c1
= new FileSystemXmlApplicationContext("D:\\along_he\\spring_study\\src\\main\\resources\\beans.xml");
UserService u1
= (UserService
) c1
.getBean("userService");
u1
.add();
BeanFactory bf
= new XmlBeanFactory(new FileSystemResource("D:\\along_he\\spring_study\\src\\main\\resources\\beans.xml"));
UserService u2
= (UserService
) bf
.getBean("userService");
u2
.add();
}
BeanFactory与ApplicationContext的区别
BeanFactory,第一次getBean时才会初始化Bean,采用了延迟加载的方式 ApplicationContext是BeanFactory的扩展,提供了更多的服务功能,及时加载。
装配Bean
1.在Beans.xml文件中写一个
<bean id="userService" class="com.hetl.day01.impl.UserServiceImpl">
<property name="name" value="李四"/>
</bean>
2.通过静态工厂
<bean id="userService1" class="com.hetl.day01.factory.UserServiceFactory" factory-method="creatUserService">
</bean>
public class UserServiceFactory {
public static UserService
creatUserService(){
return new UserServiceImpl();
}
}
@Test
public void test03(){
ApplicationContext c
= new ClassPathXmlApplicationContext("beans.xml");
UserService u
= (UserService
) c
.getBean("userService1");
u
.add()
}
3.通过实例工厂的方法
<bean id="factory" class="com.hetl.day01.factory.UserServiceFactory" />
<bean id="userService2" factory-bean="factory" factory-method="creatUserService"></bean>
public class UserServiceFactory {
public UserService
creatUserService(){
return new UserServiceImpl();
}
}
@Test
public void test03(){
ApplicationContext c
= new ClassPathXmlApplicationContext("beans.xml");
UserService u
= (UserService
) c
.getBean("userService2");
u
.add()
}