..
Spring 專(zhuān)門(mén)負(fù)責(zé)生產(chǎn)Bean,一個(gè)容器
- Tip:
一開(kāi)始我就用maven配置的spring-boot
下載太慢在maven的conf/settings.xml中設(shè)置阿里云的鏡像
<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">
<!-- 開(kāi)啟注解 -->
<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("絕對(duì)路徑");
UserAction userAction = context.getBean(UserAction.class);//通過(guò)class加載
// UserAction userAction1 = (UserAction) context.getBean("userAction");//通過(guò)id加載
3.說(shuō)明:
- 解析xml文件,獲取id、類(lèi)名、屬性;通過(guò)反射,用類(lèi)型創(chuàng)建對(duì)象。
- bean的作用域
scope: 單例singleton;多例prototype
4.bean生命周期
借張圖 ——> todo:

20180608112517790.png
public class BeanProcessor implements BeanPostProcessor {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + "實(shí)例前");
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println(beanName + "實(shí)例后");
return bean;
}
}
2.注解配置: 取代xml配置文件
@Component 取代<bean class="">
@Component("id")
web開(kāi)發(fā)中根據(jù)三層架構(gòu)提供了3個(gè)@Component衍生注解
@Controller web層,UI層
@Service service層,業(yè)務(wù)層(Business Logic)
@Repository dao層,數(shù)據(jù)層(Data Access)
@Autowired 自動(dòng)根據(jù)數(shù)據(jù)類(lèi)型注入(如果是接口,從容器找實(shí)現(xiàn)類(lèi);如果是類(lèi),查找類(lèi))
@Qualifier("id") 指定自動(dòng)注入的id名稱(chēng)
@Resource(name = "id") = @Autowired + @Qualifier("id")
@PostConstruct 自定義初始化
@PreDestroy 自定義銷(xiāo)毀
- UI層
@Controller
//@Scope("prototype") //多例,默認(rèn)單例
public class UserAction {
// @Autowired
// @Qualifier("userService")
@Resource(name = "userService")
private UserService userService;
public void addUser(){
System.out.println("用戶(hù)操作添加用戶(hù)->");
userService.addUser();
}
@PostConstruct
private void myInit() {
System.out.println("自定義初始化方法");
}
@PreDestroy
private void myDestroy() {
System.out.println("自定義銷(xiāo)毀");
}
}
- 服務(wù)層
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
public void addUser() {
System.out.println("服務(wù)添加用戶(hù)");
userDao.addUser();
}
}
- 數(shù)據(jù)層
@Repository
public class UserDaoImpl implements UserDao {
@Override
public void addUser() {
System.out.println("數(shù)據(jù)庫(kù)添加用戶(hù)");
}
}