-
AOP,面向切面編程,垂直與業(yè)務(wù)功能,實現(xiàn)方式:預(yù)編譯和運行期動態(tài)代理
相關(guān)概念:
切面:一個關(guān)注點的模塊化,可能橫切多個對象
連接點:程序執(zhí)行過程中某個特定的點
通知(Advice):在切面的某個特定連接點上執(zhí)行的動作
切入點:AOP中通知和一個切入段表達式關(guān)聯(lián)
引入:不修改代碼的前提下,為類添加新的方法和屬性
AOP代理:AOP框架創(chuàng)建的對象,用來實現(xiàn)切面契約
織入:把切面連接到其他的應(yīng)用程序類或?qū)ο笊?,并?chuàng)建一個被通知的對象
有接口的AOP代理使用JavaSE動態(tài)代理,無接口的AOP使用CGLIB代理
查看全部 -
autowirng 自動裝配,跟注入的概念相似,這里是自動的賦值引用?
xml中定義default-autowire 可以使用byName byType constructor等
查看全部 -
spring Bean可以實現(xiàn)相應(yīng)的aware接口,之后在該Bean對象中就可以獲取到IOC容器中的一些資源
查看全部 -
Bean的生命周期
定義、初始化、使用、銷毀
初始化和銷毀都有兩種方法:
直接實現(xiàn)spring中某個接口如 DisableBean,覆蓋destroy方法
配置init-method 或destroy-method,指向某個方法
或 配置全局的default intitle-method(優(yōu)先級最低,其他兩種方法配置了的時候,默認方法無效)
查看全部 -
Bean的scope:默認singleton
singleton一個Bean容器中只存在一份實例
prototype:每次請求都會創(chuàng)建新的實例 destroy方法無效
request:每次http請求創(chuàng)建一個實例且僅在當前的request內(nèi)有效
session和global session
查看全部 -
spring注入就是啟動spring容器時加載bean配置的時候完成對變量的賦值,只要是針對A類中引用B類的時候。
常用注入方式:設(shè)值注入,xml配置property,A類設(shè)置set方法
構(gòu)造注入,xml配置constructor- arg,A類設(shè)置相應(yīng)的構(gòu)造器
查看全部 -
@Qualifier:一般和@Autowired一起使用,按類型自動注入時可能會有多個Bean實例的情況,可以使用@Qualifier注解縮小范圍(或指定唯一),也可用于指定單獨的構(gòu)造器或方法參數(shù)。
代碼:
@Component(value="invoker")
public class InterfaceInvoker {
private List<InjectInterface> list;
@Autowired()
@Qualifier(value="one")
public void setList(List<InjectInterface> list) {
this.list = list;
}
public void say(){
if(null!=list){
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}else{
System.out.println("list為null");
}
}
}
@Component(value="one")
public class InterfaceImplOne implements InjectInterface {
}
@Component
public class InterfaceImplTwo implements InjectInterface {
}
結(jié)果:
injectList.InterfaceImplOne@54266750
xml文件中實現(xiàn)@Qualifier功能(不通過@Component)
查看全部 -
一、可以使用@Autowired注解注入Spring提供的解析依賴性接口(ClassPathXmlApplicationContext、AnnotationApplicationContext、BeanFactory、Envieonment、ResourceLoader、ApplicatonEventPublisher、MessageSource)
二、可以對數(shù)組(集合)類型的屬性進行注入,注入的是ApplicationContext中所有符合該類型或者是該類型的子類的(List<String>,則會把所有String類型的Bean注入List集合中)。
Key:Bean的Id。
Value:Bean的對象。
如果希望數(shù)組有序
1、Bean實現(xiàn)org.springframework.core.Ordered接口
2、@Order注解(只針對List)
InterfaceImplTwo添加@Order(value=1)
InterfaceImplOne添加@Order(value=2),先取出Two的實例,再取出One的實例。
@Autowired是由Spring BeanPostProcessor處理的,所以不能在配置類中使用它,也就是說要注入集合類型的屬性,這些屬性的值只能是通過xml或者在配置類中使用@Bean注解加載。
注入List<BeanInterface>代碼:
public interface InjectInterface {
}
@Component
public class InterfaceImplOne implements InjectInterface {
}
@Component
public class InterfaceImplTwo implements InjectInterface {
}
@Configuration
@ComponentScan("injectList")
public class InterfaceConfig {
}
@Component(value="invoker")
public class InterfaceInvoker {
private List<InjectInterface> list;
@Autowired
public void setList(List<InjectInterface> list) {
this.list = list;
}
public void say(){
if(null!=list){
for (int i=0;i<list.size();i++) {
System.out.println(list.get(i));
}
}else{
System.out.println("list為null");
}
}
}
@Test
public void test(){
ApplicationContext ac=new AnnotationConfigApplicationContext(InterfaceConfig.class);
InterfaceInvoker ii=ac.getBean("invoker",InterfaceInvoker.class);
ii.say();
}
結(jié)果:
injectList.InterfaceImplOne@39f46204
injectList.InterfaceImplTwo@5b4f1255
注入Map類型<String,InjectInterface>類型屬性(和List相似)
代碼:
@Component(value="invoker")
public class InterfaceInvoker {
private Map<String,InjectInterface> map;
@Autowired
public void setMap(Map<String, InjectInterface> map) {
this.map = map;
}
public void say(){
if(map!=null&&map.size()>0){
for (Entry<String,InjectInterface> ii: map.entrySet()) {
System.out.println(ii.getKey()+" ? ? ?"+ii.getValue());
}
}
}
}
查看全部 -
Spring之@Autowired注解
@Required:適用于Bean屬性的set方法上,bean屬性必須在配置時被填充,通過bean定義或自動裝配一個明確的屬性值。(不常用)
@Autowired:自動注入,一般標識在構(gòu)造器、set方法、成員變量上。如果找不到注入的實例,則拋出異常,可以通過required=true屬性來避免。(如果使用required屬性,則使用成員變量時應(yīng)進行判斷是否為空)
注意:每個類的構(gòu)造器都可以使用@Autowired注解,但只能有一個構(gòu)造器被標記為Autowired(required=true),required默認為false,這種情況下(解決多個構(gòu)造器不能使用required屬性),@Autowired的必要屬性,建議使用@Required注解來代替。
代碼:
public interface InjectDao {
public void save(String ss);
}
public interface InjectService {
public void say(String s);
}
@Repository(value="dao")
public class InjectDaoImpl implements InjectDao {
@Override
public void save(String ss) {
System.out.println("dao保存了"+ss+":"+hashCode());
}
}
@Service(value="service")
public class InjectServiceImpl implements InjectService {
@Autowired
private InjectDaoImpl idi;
@Override
public void say(String s) {
System.out.println("service接受了參數(shù)"+s);
idi.save(s);
}
}
@Configuration
@ComponentScan("Bean")
public class StoreConfig {
}
測試:
@Test
public void test(){
ApplicationContext ac=new AnnotationConfigApplicationContext(StoreConfig.class);
InjectServiceImpl isi=ac.getBean("service",InjectServiceImpl.class);
isi.say("今天下雨了");
}
結(jié)果:
service接受了參數(shù)今天下雨了
dao保存了今天下雨了:235283278
構(gòu)造器注入:
@Autowired
public InjectServiceImpl(InjectDaoImpl idi) {
super();
this.idi = idi;
}
set注入:
@Autowired
public InjectServiceImpl(InjectDaoImpl idi) {
super();
this.idi = idi;
}
查看全部 -
@Scope和@Bean
@Bean:默認是單例模式。
@Scope:value屬性指定Bean的作用域范圍,proxyMode屬性指定使用的代理方式(包括接口的代理和類的代理)。
代理方式主要有兩種:針對接口的代理、針對類的代理,實現(xiàn)方式有所區(qū)別。前者是jdk動態(tài)代理,后者是cglib代理。
proxyMode:容器啟動時bean還沒創(chuàng)建?通過cglib代理這個接口或者類注入到其它需要這個bean的bean中
查看全部 -
Java容器注解@ImportResource和@Value
一、通過xml文件進行配置。
<context:property-placeholder ?location="資源文件的存放位置"/>:<beans>進行配置,作用是加載資源文件(properties——Key-Value類型資源文件),然后就可以通過${Key},引用資源文件中的內(nèi)容(如數(shù)據(jù)庫的加載)。
代碼:
public class Store {
public Store(String username,String password,String url){
System.out.println(username);
System.out.println(password);
System.out.println(url);
}
<context:property-placeholder location="classpath:/config.properties"/>
<bean id="store" class="Bean.Store">
<constructor-arg name="username" value="${jdbc.username}"></constructor-arg>
<constructor-arg name="password" value="${jdbc.password}"></constructor-arg>
<constructor-arg name="url" value="${jdbc.url}"></constructor-arg>
</bean>
測試:
@Test
public void test(){
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
Store s=ac.getBean("store",Store.class);
System.out.println(s);
}
二、通過注解進行配置前提xml已經(jīng)編寫(配置類中添加@ImportResource("classpath:config.xml") )。
步驟1:配置類中添加@ImportResource,配置類中定好要用到的屬性,并在屬性上添加@Value("$(Key)")。
代碼:
@Configuration
@ImportResource("classpath:/spring-ioc.xml")
public class StoreConfig {
@Value("${url}")
private String url;
@Value("${username}") ? ? ? ? ???//取到的是當前操作系統(tǒng)的用戶名,所以不要使用username這個名。
private String username;
@Value("${password}")
private String password;
@Bean(name="store")
public Store getStoreBean(){
return new Store(username,password,url);
}
}
步驟二:這種注入依賴于使用@Bean標簽,并返回有參構(gòu)造函數(shù)對象,通過參數(shù)對Bean進行注入。
代碼:xml代碼:
? ? ? ? <beans>標簽里添加xmlns: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" ,
<beans>中添加
<context:property-placeholder location="classpath:/config.properties"/>
java代碼:
public class Store {
public Store(String username,String password,String url){
System.out.println(username);
System.out.println(password);
System.out.println(url);
}
配置文件:
username=\u5C0F\u660E
password=123456
url=localhost
查看全部 -
Java的容器注解說明——@Bean
@Bean:由SpringIoc容器配置和初始化對象,類似于XML配置文件的<bean/>,。(一般@Bean 需要在配置類中使用,即類上需要加上@Configuration注解)
@Bean的name屬性:可以通過name屬性指定Bean的Id,相當于XML中<bean>的Id。
@Component:如果一個類上標識該注解,表示配置類可以通過@ComponentScan("路徑名")掃描到該類,并實例化這個類。
舉例:
@Bean的initMethod屬性、destroyMethod屬性:通過@Bean實例化Bean時要執(zhí)行的初始化和銷毀邏輯。
?
查看全部 -
@Autowired(required=false):如果找不到注入的實例,則不會拋出異常。
注意:每個類只能有一個構(gòu)造器被標記為required=true。
查看全部 -
@Component:通用型注解,可用于任何Bean。
@Repository,@Service,@Controller是更有針對性的注解。
@Repository:通常用于注解DAO層,即持久層。
@Service:通常用于注解Service層,即服務(wù)層。
@Controller:通常用于Controller層,即控制層(MVC).
<context:component-scan ?base-package="掃描的路徑"/>
查看全部 -
Resources:針對資源文件的統(tǒng)一接口,通過Spring加載一些資源文件的時候,可以通過它去控制。
——UrlResource:URL對應(yīng)的資源,根據(jù)一個URL地址即可構(gòu)建Resources。
——ClassPathResoure:獲取類路徑下的資源文件(相對路徑)。
——FileSystemResource:獲取文件系統(tǒng)里面的資源(絕對路徑)。
——ServletContextResource:ServletContext封裝的資源,用于訪問ServletContext環(huán)境下的資源。(和Web相關(guān)的資源文件的入口)
——InputStreamResource:針對于輸入流封裝的資源。(構(gòu)建它需要InputStream)
——ByteArrayResource:針對于字節(jié)數(shù)組封裝的資源。(構(gòu)建它需要ByteArray)
ResourceLoader:對Resource加載的一個類,在SpringIOC容器中,所有的ApplicationContext都實現(xiàn)了ResourceLoader接口,所有的ApplicationContext都可以用來獲取Resource實例,所以可以通過getResource(String location)方法獲取資源Resource。
ResourceLoader接口的聲明(有個方法,輸入為文件的位置,返回的是Resource的實例)
ResourceLoader注入?yún)?shù)時前綴的幾種類型
ResourceLoader前綴:classpath:(相對路徑,加載文件)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?file:(絕對路徑,加載文件)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?url:??http(web路徑、加載文件)
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? (none):直接輸入路徑,依賴ApplicationContext
案例:(Bean通過實現(xiàn)ApplicationContext接口,間接的實現(xiàn)了ResourceLoader接口(加載Resource實例),就可以使用getResource()獲取Resource的實例,Resource擁有一系列的方法,比如獲取文件名稱(getFilename()和獲取文件長度contentLength())
步驟1:
public class ResourceDemo implements ApplicationContextAware {
private ApplicationContext ac;
@Override
public void setApplicationContext(ApplicationContext ac) throws BeansException {
this.ac=ac;
}
public void resource() throws IOException{
Resource r=ac.getResource("classpath:resourceDemo.txt");(直接寫文件,而不寫全路徑,是因為Java build path 配置了source,所以這里是相對路徑)
System.out.println(r.getFilename());
System.out.println(r.contentLength());
}
}
步驟2:
? ?<bean id="resourceDemo" class="ResourceDemo" ></bean>
步驟3:
@Test
public void testBean() throws IOException{
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");
rd.resource();
}
測試:
@Test
public void testBean() throws IOException{
ApplicationContext ac=new ClassPathXmlApplicationContext("spring-ioc.xml");
ResourceDemo rd=(ResourceDemo)ac.getBean("resourceDemo");
rd.resource();
}
結(jié)果:(文件:resourceDemo.txt,在src——>resource文件夾下)
resourceDemo.txt
6
案例:file方式
案例:url方式
查看全部
舉報