註解功能
1)配置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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">java
<context:annotation-config/>
<context:component-scan base-package="com.fz.entity"/>
</beans>
2)註解使用
@Component 若是不寫,則使用類名小寫做爲名稱mysql
@Component("uuu")spring
@Data @Component
public class Book {
@Value("100")
private int id;sql
@Value("《mysql數據庫技術》")
private String name;
}數據庫
@Autowired 根據類型自動裝配this
@Autowired //根據類型自動裝配 byTypeprototype
@Autowired @Qualifier("user") //根據名稱自動裝配 byName
private User user;component
@Resource(name="book")xml
3)註解bean類方法
註解bean類 com.entity.Db類
---------------------------------------------------
@Component("db")
public class Db {
public void show(){
System.out.println("dbshow");
}
}對象
@Controller("member")
public class Member {
public void save(){
System.out.println("saveok");
}
}
@Service("m")
public class Member {
public void save(){
System.out.println("saveok");
}
}
@Repository("mm")
public class Member {
public void save(){
System.out.println("saveok");
}
}
4)註解單例 多例
---------------------------------------------------
@Repository("mm") @Scope("singleton") 單例默認
public class Member {
}
@Repository("mm") @Scope("prototype") 多實例
public class Member {
}
5)註解初始化方法
import javax.annotation.PostConstruct;
---------------------------------------------------
@PostConstruct
public void a(){
System.out.println("初始化1");
}
@PostConstruct
public void init(){
System.out.println("初始化2");
}
6)註解銷燬方法
import javax.annotation.PreDestroy;
---------------------------------------------------
@PreDestroy
public void close(){
System.out.println("退出");
}
@PreDestroy
public void exit(){
System.out.println("銷燬");
}
7)屬性注入
-------------------------------------------------------
@Data
@Component("book")
@Scope("prototype")
public class Book {
@Value("BS101")
private String bid;
@Value("java書籍")
private String bname;
}
@Data @Repository("mm") @Scope("prototype")
public class Member {
@Resource(name="book") 此處的book 就是指定工廠中的 上的那book名@Component("book")
至關於beans.xml 中的<property name="" ref="u"></property>
private Book book;
}
@Data @Repository("mm") @Scope("prototype")
public class Member {
@Autowired 根據類型自動注入對象
private Book book;
}
8)延遲加載
-----------------------------------------------------------------
單例模式是默認的,會當即加載 加上@Lazy 不自動加載,第一次使用時再加載
@Data
@Component @Scope("singletone") @Lazy
public class Teacher {
private String name;
public Teacher() {
System.out.println("空構造方法");
}
public Teacher(String name) {
this.name = name;
System.out.println("有參數name的構造方法");
}
public void info(){
System.out.println("老師姓名:"+this.name);
}
}
@Scope("prototype") 多實例,默認使用的使用時加載
@Data @Repository("mm") @Scope("singleton") @Lazy 延遲加載,在第一次使用時加載
public class Member {
}
@Data @Repository("mm") @Scope("singleton") @Lazy(false) 當即加載初始化
public class Member {
}
beans.xml
<beans default-lazy-init="true"> 全部bean都延遲實例化初始化加載
<beans default-autowire="byName">默認注入相關屬性對象