1.Spring的Bean管理的中常用的註解
@Component:組件.(作用在類上)
public interface UserDao { public void sayHello(); } @Component(value="userDao") public class UserDaoImpl implements UserDao { @Override public void sayHello() { System.out.println("Hello Spring Annotation..."); }
Spring中提供@Component的三個衍生註解:(功能目前來講是一致的)
* @Controller :WEB層
* @Service :業務層
* @Repository :持久層
這三個註解是爲了讓標註類本身的用途清晰,Spring在後續版本會對其增強
2.屬性注入的註解:(使用註解注入的方式,可以不用提供set方法.)
@Value :用於注入普通類型.
@Autowired :自動裝配:
* 默認按類型進行裝配.
* 按名稱注入:
* @Qualifier:強制使用名稱注入.
@Resource相當於:
* @Autowired和@Qualifier一起使用.
3.Bean的作用範圍的註解:
@Scope:
* singleton:單例
* prototype:多例
4.Bean的生命週期的配置:
@PostConstruct :相當於init-method
@PreDestroy :相當於destroy-method
@PostConstruct說明
被@PostConstruct修飾的方法會在服務器加載Servlet的時候運行,並且只會被服務器調用一次,類似於Serclet的inti()方法。被@PostConstruct修飾的方法會在構造函數之後,init()方法之前運行。
@PreConstruct說明
被@PreConstruct修飾的方法會在服務器卸載Servlet的時候運行,並且只會被服務器調用一次,類似於Servlet的destroy()方法。被@PreConstruct修飾的方法會在destroy()方法之後運行,在Servlet被徹底卸載之前。
05.Spring的Bean管理的方式的比較:
XML和註解:
* XML :結構清晰.
* 註解 :開發方便.(屬性注入.)
實際開發中還有一種XML和註解整合開發:
* Bean有XML配置.但是使用的屬性使用註解注入
關於註解1:工作中有一次犯了一個很嚴重的問題:
關於註解2:想要兩個類使用同一個變量,而且兩個類有關係,通過注入方式建立兩個類的對象產生關係。但是如果想要共用一個對象,建立對象可以通過有參構造傳入(new A("xxx")),但是註解建立對象我不會傳參。但可以通過配置文件和註解相結合使用。
第二張圖演示了properties文件屬性的使用方法,在工作中,又遇見另外一種(在這裏演示)
首先,在spring的主配文件中要配置:
<!-- 加載applicationConfig.properties文件,獲取屬性文件的內容 --> <bean id="propertyConfigurer" class="com.ad.utils.GlobalProperties"> <property name="ignoreResourceNotFound" value="true" /> <property name="locations"> <list> <value>classpath:applicationConfig.properties</value> ...... </list> </property> </bean>
然後寫出實現類:
import java.util.HashMap; import java.util.Map; import java.util.Properties; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; /** * 自定義PropertyPlaceholderConfigurer返回properties內容 * */ public class GlobalProperties extends PropertyPlaceholderConfigurer{ private static Map<String, Object> ctxPropertiesMap; @Override protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) throws BeansException { super.processProperties(beanFactoryToProcess, props); ctxPropertiesMap = new HashMap<String, Object>(); for (Object key : props.keySet()) { String keyStr = key.toString(); String value = props.getProperty(keyStr); ctxPropertiesMap.put(keyStr, value); } } public static Object getContextProperty(String name) { return ctxPropertiesMap.get(name); } /** * 獲取屬性值 * @param key * @return */ public static String getProperties(String key){ Object value = ctxPropertiesMap.get(key); return value != null ? String.valueOf(value) : ""; } /** *獲取屬性值,返回××× * @param key * @return */ public static Integer getInteger(String key){ Object value = ctxPropertiesMap.get(key); return value != null ? Integer.valueOf(value.toString()) : 0; } }
最後是使用方法:
GlobalProperties.getProperties("XXXXX").trim();
補:spring的bean管理xml方式
01.注入對象類型屬性
創建service類和dao類
在service中得到dao對象