spring(讀取外部資料庫配置資訊、基於註解管理bean、DI)

來源:互聯網
上載者:User

標籤:成功   賦值   pat   prot   comm   初始   運算式   system   sys   



###解析外部設定檔
在resources檔案夾下,建立db.properties(和資料庫連接相關的資訊)

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db
username=root
password=root

開發步驟
1)建立maven工程
添加web.xml
添加tomcat運行環境
添加jar spring-webmvc,junit,commons-dbcp,mysql
添加application.xml
2)設定資料庫的串連池資訊
              

 <!-- 1.util:properties表示讀取外部的屬性檔案,並執行個體化對象2.id表示名稱3.location表示屬性檔案的位置<util:properties id="dbConf" location="classpath:db.properties">       <!--設定資料庫的串連池    1.使用spring運算式給屬性賦值   2.spring運算式文法格式:#{ }---><bean id="dataSource"class="org.apache.commons.dbcp.BasicDataSource"><bean id="dataSource"  class="org.apache.commons.dbcp.BasicDataSource">     <property name="driverClassName"       value="#{dbConf.driverClassName}"/>     <property name="url"       value="#{dbConf.url}"/>     <property name="username"       value="#{dbConf.username}"/>     <property name="password"       value="#{dbConf.password}"/> </bean>


##基於註解的bean管理
##1.基於註解的方式執行個體化對象(推薦使用)
    執行個體化對象和依賴注入有兩種方式:設定檔,註解

1.掃描包  (設定檔)
<!--掃描包可以掃描當前包和子包下的所有類-->
     <context:component-scan  base-package="cn.tedu.dao">

2.特定功能(執行個體化功能)的註解
      @Component通用註解:執行個體化對象
      @Controller:執行個體化控制層類的對象
      @Service:執行個體化業務層類的對象
      @Reponsitory:執行個體化持久層類的對象

 @Repository("userDao")      public class UserDaoImpl implements UserDao{           public void insertUser(){                 System.out.println("添加成功!");        }

//在mybatis裡,一般是定義一個介面,介面裡面定義抽象方法,並在設定檔裡面實現相應的抽象方法。

 

##2.生命週期管理

//@Component表示執行個體化對象@Componentpublic class BeanLife {public BeanLife(){System.out.println("BeanLife");}//@PostConstruct表示定義初始化方法//@PostConstruct:Tomcat運行環境依賴的jar包@PostConstructpublic void init(){System.out.println("init");}public void execute(){System.out.println("execute");}//@PreDestroy表示定義銷毀的方法//@PreDestroy:Tomcat運行環境依賴的jar包@PreDestroypublic void destroy(){System.out.println("destroy");}}

 



##3.bean範圍

//通過註解執行個體化對象,預設為單例singleton//@Scope定義bean的範圍//@Scope("prototype")表示bean範圍為多例@Component@Scope("prototype")public class DemoScope {}

 


##4.消極式載入

//預設bean對象的執行個體化,是立即載入
//@Lazy設定bean是否消極式載入的註解
//@Lazy(true)設定bean對象為消極式載入

@Component@Lazy(true)public class DemoLazy {public DemoLazy(){System.out.println("DemoLazy");}}

 



#DI(動態地向某個對象提供它所要的對象)

參考:http://www.360doc.com/content/18/0125/09/27831725_724899826.shtml
##[email protected](推薦使用)                        

//[email protected]  tomcat運行環境依賴jar包中定義的註解
//[email protected] 實現依賴注入
//[email protected] 實現依賴注入,可以省略set方法
//[email protected]預設依賴注入的方式為byName
//[email protected]如果沒有匹配的屬性
//               按照byType方式實現依賴注入
//[email protected](name="userDaoImpl")
   

  @Resource(name="userDaoImpl")     private UserDao userDao;


       

##[email protected](瞭解)                             

public class UserServiceImpl2 implements UserService2{
//[email protected] 依賴注入
//[email protected] 預設依賴注入的方式byType;
// 如果有多個相同類型的對象,那麼按照byName依賴注入
//3.如果使用byName實現依賴注入,
//  使用@Qualifier註解定義匹配的名稱
//[email protected]不能單獨使用

@Autowired@Qualifier("userDaoImpl")private UserDao userDao;public void addUser() {userDao.insertUser();}}

 


##[email protected](瞭解)

@Componentpublic class Student {//@Value("Admin")給String或者基礎資料型別 (Elementary Data Type)依賴注入//@Value("#{conf.name}")使用spring運算式實現依賴注入@Value("#{conf.name}")private String name;public String toString(){return "name="+name;}}

 

spring(讀取外部資料庫配置資訊、基於註解管理bean、DI)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.