[email protected]註解與省去get和set方法,
<?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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.3.xsd"> <bean id="autotest" class="autotest" autowire="constructor"> </bean> <!-- 當 Spring 容器啟動時,AutowiredAnnotationBeanPostProcessor 將掃描 Spring 容器中所有 Bean,當發現 Bean 中擁有 @Autowired 註釋時就找到和其匹配(預設按型別匹配)的 Bean,並注入到對應的地方中去 --> <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> <bean id="person" class="Person"> <property name="age" value="21"></property> <property name="name" value="李躍"></property> <property name="sex" value="男"></property> </bean> </beans>
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class autotest { @Autowired//這裡用了這個註釋就省去了構造方法或者get,set方法來設定person屬性 private Person person; public void fun(){ ConfigurableApplicationContext cf=new ClassPathXmlApplicationContext("spring-mvc.xml"); autotest autotest=(autotest)cf.getBean("autotest"); System.out.println(autotest); System.out.println(autotest.person); } public static void main(String args[]){ new autotest().fun(); } }
否則如下
<?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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util" xmlns:websocket="http://www.springframework.org/schema/websocket" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.3.xsd"> <bean id="autotest" class="autotest" > <property name="person" ref="person"></property> </bean> <bean id="person" class="Person"> <property name="age" value="21"></property> <property name="name" value="李躍"></property> <property name="sex" value="男"></property> </bean> </beans>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class autotest {
private Person person;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public void fun(){
ConfigurableApplicationContext cf=new ClassPathXmlApplicationContext("spring-mvc.xml");
autotest autotest=(autotest)cf.getBean("autotest");
System.out.println(autotest);
System.out.println(autotest.person);
}
public static void main(String args[]){
new autotest().fun();
}
}
2.對註解Autowired放在setter方法上的情況,這種情況要注意幾種情況,先看程式碼
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:websocket="http://www.springframework.org/schema/websocket"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd
http://www.springframework.org/schema/websocket http://www.springframework.org/schema/websocket/spring-websocket-4.3.xsd">
<bean id="autotest" class="autotest"/>
<!--
當 Spring 容器啟動時,AutowiredAnnotationBeanPostProcessor 將掃描 Spring 容器中所有 Bean,當發現 Bean 中擁有 @Autowired 註釋時就找到和其匹配(預設按型別匹配)的 Bean,並注入到對應的地方中去
-->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
<bean id="person" class="Person">
<property name="age" value="21"></property>
<property name="name" value="李躍"></property>
<property name="sex" value="男"></property>
</bean>
</beans>
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//A:
public class autotest{
//C:
private Person person;
public void fun(){
ConfigurableApplicationContext cf=new ClassPathXmlApplicationContext("spring-mvc.xml");
autotest autotest=(autotest)cf.getBean("autotest");
System.out.println(autotest.person); //[email protected]
System.out.println(this); //[email protected]
System.out.println(autotest); //[email protected]
System.out.println(this.person);//null,
}
public Person getPerson() {
System.out.println(person);
return person;
}
@Autowired
public void setPerson(Person person) {
this.person = person;
}
public static void main(String args[]){
new autotest().fun(); // (new autotest()==B)呼叫A==b.a.fun()
}
}
上面要注意區分幾個物件this,autotest,是有區別的,對this和autotest來說person都是他們的屬性,但是真正通過註解和配置檔案配合工作的卻是autotest,所以對於註解在setter方法上的情況,只有autotest.person不是空,單獨的this.person或者new 一個物件.person是空的。這裡我是來自http://blog.csdn.net/badboy_qiao/article/details/52713257最後總結的啟發。
結論:注入的物件,只會呼叫空參建構函式,且這個物件的所有屬性都是預設值,自己手動賦予的值不會被使用,
所以在A中的C類,在B中呼叫A的時候,建立的A物件的屬性都是預設值,所以A物件雖然有了,但是A的屬性C卻是null,所以在B中直接this.a.c.method()是會報null指標異常的,且是c是null的發生原因。
解決方式:C在A中新增get方法,然後B中使用a.getC()即可獲得c的物件,且c的物件也是spring注入的。
再順便談一個問題做下記錄,為了更好理解上面的幾句話看下面物件的輸出,this物件始終和第一次真正new出來的物件(autotest test=new autotest();不是new autotest();)是同一個東西
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
//A:
public class autotest{
//C:
private Person person;
public void fun(){
ConfigurableApplicationContext cf=new ClassPathXmlApplicationContext("spring-mvc.xml");
autotest autotest=(autotest)cf.getBean("autotest");
autotest test=new autotest();
System.out.println(test);//D [email protected]
System.out.println(this); //C [email protected]
System.out.println(this.person);//null
System.out.println(test.person);//null
System.out.println(autotest.person);//[email protected]
}
public Person getPerson() {
System.out.println(person);
return person;
}
@Autowired
public void setPerson(Person person) {
this.person = person;
}
public static void main(String args[]){
System.out.println(new autotest());//B [email protected]
autotest test=new autotest();
System.out.println(test.person);//null
System.out.println(test);//A [email protected]
test.fun();
}
}
還有一個地方就是如果所有bean在容器中已經存在,那麼我們某個Java類中如果用到其它物件,我們可以省去在當前這個Java類的bean配置檔案中用property來配置它包含的屬性,它會自動注入這些屬性物件,直接使用就行。
如下面這裡使用到這個物件
這個類的bean中可以直接
<bean name="shopManagerController" class="com.xxxx.ShopManagerController"">
</bean>不用property來配置shopManagerService,可以省去,用了resource註解settter方法也可以省去,但是要在介面實現類或者類的上面使用@Service("resource右邊的name或者介面或者類的首字母小寫")
<!-- 把標記了@Controller等註解的類轉換為bean -->
<context:component-scan base-package="com.XXX" />
XML中也不需要配置controller的bean
1.bean的例項化,個人理解
1.通過自動掃包,註解方式配置bean,不用繁瑣的在xml檔案中進行配置很多bean,
這種bean當tomcat啟動專案的時候,bean物件是null,當我呼叫相應方法時,
它會首先被例項化然後被呼叫,如下面只有當我我呼叫selectpage方法時,handle通過afterPropertiesSet方法呼叫子類的
的initService方法被例項化,然後handle物件不再為空
protected abstract void initService();
@Override
public void afterPropertiesSet() throws Exception {
initService();
System.out.println("初始化handle"+handle);
if (handle == null) {
logger.info("Generic handle is not set. Please set it in the initService method if handle access is desired!");
}
}
public SearchCriteria<T> selectPage(SearchCriteria<T> sc) {
return handle.selectPage(sc);
}
2.xml中配置bean,這種bean當tomcat啟動專案的時候,bean物件就被例項化放入到一個容器中,當我呼叫相應方法時,
它會被直接取出來,如上面當我我呼叫selectpage方法時,不需要再進行afterPropertiesSet方法呼叫子類的
的initService方法對handle進行例項化,然後handle物件不會為空。
相關推薦
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fdae8d8f94939abdbc8889928a948f9899">[email160;protected]a>註解與省去get和set方法,
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/
shell腳本中的$# $0 <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f8dcb8">[email160;protected]a> $* $$ $! $?的意義
腳本 $* width 上一個 pre shell int .cn height 轉載自:http://www.cnblogs.com/davygeek/p/5670212.html 今天學寫腳本遇到一些變量不認識,在此做下記錄。 變量 含義 $0 當前腳本的文件
shell中$*與<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b296f2">[email160;protected]a>的區別
劃分 位置 一個 這也 差異 獨立 [email protected] 情況 雙引號 $*所有的位置參數,被作為一個單詞 註意:"$*"必須被""引用 [email protected] 與$*同義,但是每個參數都是一個獨立的""引用字串,這就意味著參數
Spring4.0系列<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aa9f87eae9c5c4cec3dec3c5c4cbc6">[email160;protected]a>
one window 標識 cto ace ted ada bsp 布爾 這篇文章介紹Spring 4的@Conditional註解。在Spring的早期版本你可以通過以下方法來處理條件問題: 3.1之前的版本,使用Spring Expression Langua
Spring高級話題<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b29ff2f7dcd3d0ded7">[email160;protected]a>***註解的工作原理
sso metadata bool logs tcl task ota -c ann 出自:http://blog.csdn.net/qq_26525215 @EnableAspectJAutoProxy @EnableAspectJAutoProxy註解 激活Aspe
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="297a595b40474e69685c5d465e405b4c4d">[email160;protected]a>註解與自動裝配(轉發)
配置 調用方法 support autowired 信息 ann over 反射機制 test 1 配置文件的方法我們編寫spring 框架的代碼時候。一直遵循是這樣一個規則:所有在spring中註入的bean 都建議定義成私有的域變量。並且要配套寫上 get 和 se
linux bash Shell特殊變數:Shell $0, $#, $*, <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8aaeca">[email160;protected]a>, $?
在linux下配置shell引數說明 前面已經講到,變數名只能包含數字、字母和下劃線,因為某些包含其他字元的變數有特殊含義,這樣的變數被稱為特殊變數。 例如,$ 表示當前Shell程序的ID,即pid,看下面的程式碼: [[email protected] /]$ ec
spring <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="62000d0d16222103010a0703000e07">[email160;protected]a>中value的理解
先看原始碼 /** * Names of the caches in which method invocation results are stored. * <p>Names may be used to determine the target cache (or cac
{<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="733e3c3f3f2a342136363d203323213c273c3d3e323a3f5d303c3e">[email160;protecte
近日,復旦解密安全團隊發現GandCrab4.0活躍度提升,跟蹤到多起GandCrab4.0變種勒索事件,現釋出安全預警,提醒廣大使用者預防GandCrab4.0勒索。 目前復旦解密已經可以成功解密GandCrab4.0變種採用RSA+AES加密演算法 mg中毒檔案可以在一個小時解決.電話151691214
Springboot註解<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="260b0b666549485254494a4a4354">[email160;protected]a>和@RestCon
1.使用@Controller 註解,在對應的方法上,檢視解析器可以解析return 的jsp,html頁面,並且跳轉到相應頁面;若返回json等內容到頁面,則需要加@ResponseBody註解 [email protected]註解,相當於@[email protected
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5b2c3e391b33">[email160;protected]a>,c小總結
問題0:元素內聯元素,行內元素,行內塊元素. 內聯: 寬高M,P都有效 行內元素:無寬高,內容撐開,M,P左右有效
SQL Server資料庫mdf檔案中了勒索病毒<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="fc9f8e858c889998a39d8f9d9293bc9f939f97">[email160;p
SQL,資料庫,勒索病毒,mdf檔案中毒,[email protected]_email *SQL Server資料庫mdf檔案中了勒索病毒[email protected]_email。副檔名變為[email protected]_email SQL Serv
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5400313a273b2632383b2379142032">[email160;protected]a>_export詳解
Tensorflow經常看到定義的函式前面加了“@tf_export”。例如,tensorflow/python/platform/app.py中有: @tf_export('app.run') def run(main=None, argv=None): """Runs the progr
手把手教你搭建React Native 開發環境 - ios篇 (React <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="eda38c99849b88adddc3d8d8c3d9">[email
由於之前我是h5的,沒接觸過ios和安卓, 也不瞭解xcode配置,所以 建議學reace-native之前還是先去了解一下ios和安卓開發環境搭建等問題。 環境下載及配置 nodejs:https://nodejs.org/en/download/ 設定淘寶映象 $ npm con
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4978382833093e3a3179797878">[email160;protected]a>
function changeSpan(){ var f = document.getElementById("file1").files; &nb
SpringBoot學習<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b7e8f7e7c5d8c7d2c5c3cee4d8c2c5d4d2">[email160;protected]a>&am
文章目錄 @PropertySource:載入指定的配置檔案 @ImportResource:匯入Spring的配置檔案,讓配置檔案裡面的內容生效; @Bean @PropertySource:載入指定的配置檔案
eclipse支援@<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="95d2f0e1e1f0e7d5c6f0e1e1f0e7">[email160;protected]a>註解使用 -轉載
1. 下載lombok.jar 2.將下載的lombok.jar放在你的eclipse安裝目錄下,如圖: 3.修改eclipse.ini檔案,新增如下兩行配置: -Xbootclasspath/a:lombok.jar -javaage
無法解析的外部符號 <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e41497770537f77705e2f28">[email160;protected]a>,該符號在函式 ___tmai
#include using namespace std; int main() { cout <<“This is a C++ program.”; return 0; } 1>------ 已啟動生成: 專案: hello1, 配置: Debug Win32 ---
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ce9eb7baa6a1a08ea4afb8af">[email160;protected]a>@C 比較
對所有的程式語言,他們的最後的目的其實就是兩種:提高硬體的執行效率和提高程式設計師的開發效率。 遺憾的是,這兩點是不可能並存的!你只能選一樣。在提高硬體的執行效率這一方面,C語言沒有競爭者!舉個簡單的例子,實現一個列表,C語言用陣列int a[3],經過編譯以後變成了(基地址+偏移量)的方式。對
<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="762506041f18113b203529363b1912131a370202041f14030213">[email160;protected]<
有 @ModelAttribute 標記的方法, 會在每個目標方法執行之前被 SpringMVC 呼叫 <form action="springmvc/TestModelAttribute" method="post"> 使用者名稱<input