..
Spring 专门负责生产Bean,一个容器
- Tip:
一开始我就用maven配置的spring-boot
下载太慢在maven的conf/settings.xml中设置阿里云的镜像
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
1.Spring容器加载
1.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"
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:annotation-config/>
<!-- 注解的位置 -->
<context:component-scan base-package="user"/>
<!-- 配置bean处理器 -->
<bean class="user.BeanProcessor"></bean>
<!-- xml配置模式 示例-->
<!-- <bean id="userAction" class="user.action.UserAction">-->
<!-- <property name="name" value="bobo"></property>-->
<!-- </bean>-->
</beans>
2.Spring容器加载
//注意加载路径!!! 这是target/classes下的路径!!!
ApplicationContext context = new ClassPathXmlApplicationContext("beans1.xml");
// ApplicationContext context1 = new FileSystemXmlApplicationContext("绝对路径");
UserAction userAction = context.getBean(UserAction.class);//通过class加载
// UserAction userAction1 = (UserAction) context.getBean("userAction");//通过id加载
3.说明:
- 解析xml文件,获取id、类名、属性;通过反射,用类型创建对象。
- bean的作用域
scope
: 单例singleton
;多例prototype
4.bean生命周期
借张图 ——> todo:
public class BeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + "实例前");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + "实例后");
return bean;
}
}
2.注解配置: 取代xml配置文件
@Component
取代<bean class="">
@Component("id")
web开发中根据三层架构提供了3个@Component
衍生注解
@Controller
web层,UI层
@Service
service层,业务层(Business Logic)
@Repository
dao层,数据层(Data Access)
@Autowired
自动根据数据类型注入(如果是接口,从容器找实现类;如果是类,查找类)
@Qualifier("id")
指定自动注入的id名称
@Resource(name = "id")
= @Autowired
+ @Qualifier("id")
@PostConstruct
自定义初始化
@PreDestroy
自定义销毁
- UI层
@Controller
//@Scope("prototype") //多例,默认单例
public class UserAction {
// @Autowired
// @Qualifier("userService")
@Resource(name = "userService")
private UserService userService;
public void addUser(){
System.out.println("用户操作添加用户->");
userService.addUser();
}
@PostConstruct
private void myInit() {
System.out.println("自定义初始化方法");
}
@PreDestroy
private void myDestroy() {
System.out.println("自定义销毁");
}
}
- 服务层
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void addUser() {
System.out.println("服务添加用户");
userDao.addUser();
}
}
- 数据层
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("数据库添加用户");
}
}