public class JdbcDemo1{
publicstaticvoidmain(String[] args)throws SQLException{
//1、注冊驅(qū)動
DriverManager.registerDriver(newcom.mysql.jdbc.Driver());
//2、獲取連接ConnectionConnection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/spring5","root","123456");
//3、獲取操作數(shù)據(jù)庫的預(yù)處理對象PreparedStatmentPreparedStatement pstm = conn.prepareStatement("SELECT * FROM account");
//4、執(zhí)行SQL語句,獲得結(jié)果集ResultSet resultSet = pstm.executeQuery();//
5、遍歷結(jié)果集while(resultSet.next()){ System.out.println(resultSet.getString("name")); }
//6、關(guān)閉資源resultSet.close(); pstm.close(); conn.close(); }}
工廠類對象
/********
*???一個創(chuàng)建Bean對象的工廠
*
*???第一個:需要一個配置文件來配置我們的service和dao
*??????????????配置的內(nèi)容:唯一標(biāo)識=全限定類名(key=value)
*???第二個:通過讀取配置文件中配置的內(nèi)容,反射創(chuàng)建對象
*???我的配置文件可以是xml也可以是properties
******/
public class BeanFactory {
????//定義衣蛾P(guān)roperties對象,用于存放解析的配置信息
????private static Properties props = new Properties();;
????//存儲
????private static Map<String,Object> beans = new HashMap<String, Object>();
????//使用靜態(tài)塊為props對象賦值
????static {
????????try {
????????????//讀取配置文件
????????????InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");
????????????//加載配置文件字節(jié)輸入流
????????????props.load(in);
????????????//讀取配置文件,獲取所有的key
????????????Enumeration<Object> keys = props.keys();
????????????//遍歷所有的key
????????????while (keys.hasMoreElements()){
????????????????//獲得key
????????????????String key = keys.nextElement().toString();
????????????????//獲取key對應(yīng)的類全限定名
????????????????String beanPath = (String) props.get(key);
????????????????//獲取字節(jié)碼對象
????????????????Object value = Class.forName(beanPath).newInstance();
????????????????//把key和value存入容器中
????????????????beans.put(key,value);
????????????}
????????} catch (Exception e) {
????????????e.printStackTrace();
????????}
????}
????/***
?????* 根據(jù)Bean的名稱獲取對象
?????* @param beanName
?????* @return
?????*/
????public static Object getBean(String beanName){
????????return??beans.get(beanName);
????}
}
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
</dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--
????????bean標(biāo)簽:用于配置讓spring創(chuàng)建對象,并且存入ioc容器之中
???????????id屬性:對象的唯一標(biāo)識。
???????????class屬性:指定要創(chuàng)建對象的全限定類名
????-->
????<!-- 配置service -->
????<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl"></bean>
????<!-- 配置dao -->
????<bean id="accountDao" class="com.oppo.dao.impl.AccountDaoImpl"></bean>
??//1.使用ApplicationContext接口,就是在獲取spring容器
????????ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");
????????//2.根據(jù)bean的id獲取對象
????????AccountService accountService = (AccountService) ac.getBean("accountService");
????????System.out.println(accountService);
????????AccountDao accountDao = (AccountDao) ac.getBean("accountDao");
????????System.out.println(accountDao);
BeanFactory才是Spring容器中的頂層接口。
ApplicationContext是它的子接口。
BeanFactory和ApplicationContext的區(qū)別:
????創(chuàng)建對象的時間點(diǎn)不一樣。
???????ApplicationContext:只要一讀取配置文件,默認(rèn)情況下就會創(chuàng)建對象。
???????BeanFactory:什么使用什么時候創(chuàng)建對象
???????ClassPathXmlApplicationContext:
????它是從類的根路徑下加載配置文件???????推薦使用這種
FileSystemXmlApplicationContext:
????它是從磁盤路徑上加載配置文件,配置文件可以在磁盤的任意位置。
AnnotationConfigApplicationContext:
????當(dāng)我們使用注解配置容器對象時,需要使用此類來創(chuàng)建spring容器。它用來讀取注解。
bean 標(biāo)簽
作用:
????用于配置對象讓spring來創(chuàng)建的。
????默認(rèn)情況下它調(diào)用的是類中的無參構(gòu)造函數(shù)。如果沒有無參構(gòu)造函數(shù)則不能創(chuàng)建成功。
屬性:
????id:給對象在容器中提供一個唯一標(biāo)識。用于獲取對象。
????class:指定類的全限定類名。用于反射創(chuàng)建對象。默認(rèn)情況下調(diào)用無參構(gòu)造函數(shù)。
????scope:指定對象的作用范圍。
?????????* singleton??:默認(rèn)值,單例的.
?????????* prototype??:多例的.
?????????* request????:WEB項(xiàng)目中,Spring創(chuàng)建一個Bean的對象,將對象存入到request域中.
?????????* session????:WEB項(xiàng)目中,Spring創(chuàng)建一個Bean的對象,將對象存入到session域中.
?????????* global session :WEB項(xiàng)目中,應(yīng)用在Portlet環(huán)境.如果沒有Portlet環(huán)境那么globalSession相當(dāng)于session.
????init-method:指定類中的初始化方法名稱。
????destroy-method:指定類中銷毀方法名稱
????在AccountServiceImpl總加上2個方法
//初始化時調(diào)用
public void init(){
????System.out.println("AccountServiceImpl初始化了。。。。。");
}
//銷毀時調(diào)用
public void destroy(){
????System.out.println("AccountServiceImpl銷毀了。。。。。");
}
單例對象:scope="singleton"
????一個應(yīng)用只有一個對象的實(shí)例。它的作用范圍就是整個引用。
????生命周期:
???????對象出生:當(dāng)應(yīng)用加載,創(chuàng)建容器時,對象就被創(chuàng)建了。
???????對象活著:只要容器在,對象一直活著。
???????對象死亡:當(dāng)應(yīng)用卸載,銷毀容器時,對象就被銷毀了。
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl" scope="singleton" init-method="init" destroy-method="destroy"></bean>
多例對象:scope="prototype"
????每次訪問對象時,都會重新創(chuàng)建對象實(shí)例。
????生命周期:
???????對象出生:當(dāng)使用對象時,創(chuàng)建新的對象實(shí)例。
???????對象活著:只要對象在使用中,就一直活著。
???????對象死亡:當(dāng)對象長時間不用時,被java的垃圾回收器回收了。
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl" scope="prototype"??init-method="init" destroy-method="destroy"></bean>
實(shí)例化Bean 的方法
第一種方式:使用默認(rèn)無參構(gòu)造函數(shù)
????<!--在默認(rèn)情況下:
???????它會根據(jù)默認(rèn)無參構(gòu)造函數(shù)來創(chuàng)建類對象。如果bean中沒有默認(rèn)無參構(gòu)造函數(shù),將會創(chuàng)建失敗。
????-->
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl"/>
第二種方式:spring管理靜態(tài)工廠-使用靜態(tài)工廠的方法創(chuàng)建對象
/**
* 模擬一個靜態(tài)工廠,創(chuàng)建業(yè)務(wù)層實(shí)現(xiàn)類
*/
public class StaticFactory {????
????public static AccountService createAccountService(){
???????return new AccountServiceImpl();
????}
}
<!-- 此種方式是:
?????使用StaticFactory類中的靜態(tài)方法createAccountService創(chuàng)建對象,并存入spring容器
?????id屬性:指定bean的id,用于從容器中獲取
?????class屬性:指定靜態(tài)工廠的全限定類名
?????factory-method屬性:指定生產(chǎn)對象的靜態(tài)方法
-->
<bean id="accountService"
??????class="com.oppo.factory.StaticFactory"
??????factory-method="createAccountService"></bean>
第三種方式:spring管理實(shí)例工廠-使用實(shí)例工廠的方法創(chuàng)建對象
/**
* 模擬一個實(shí)例工廠,創(chuàng)建業(yè)務(wù)層實(shí)現(xiàn)類
* 此工廠創(chuàng)建對象,必須現(xiàn)有工廠實(shí)例對象,再調(diào)用方法
*/
public class InstanceFactory {??
????public AccountService createAccountService(){
???????return new AccountServiceImpl();
????}
}
<!-- 此種方式是:
?????先把工廠的創(chuàng)建交給spring來管理。
????然后在使用工廠的bean來調(diào)用里面的方法
????factory-bean屬性:用于指定實(shí)例工廠bean的id。
????factory-method屬性:用于指定實(shí)例工廠中創(chuàng)建對象的方法。
-->
<bean id="instancFactory" class="com.oppo.factory.InstanceFactory"></bean>
<bean id="accountService"
??????factory-bean="instancFactory"
??????factory-method="createAccountService"></bean>
spring 依賴注入
????顧名思義,就是使用類中的構(gòu)造函數(shù),給成員變量賦值。注意,賦值的操作不是我們自己做的,而是通過配置的方式,讓spring框架來為我們注入。具體代碼如下:
/**
*/
public class AccountServiceImpl implements AccountService {
????private String name;
????private Integer age;
????private Date birthday;
????public AccountServiceImpl(String name, Integer age, Date birthday) {
???????this.name = name;
???????this.age = age;
???????this.birthday = birthday;
????}
????@Override
????public void saveAccount() {
???????System.out.println(name+","+age+","+birthday);????
????}
}
<!-- 使用構(gòu)造函數(shù)的方式,給service中的屬性傳值
????要求:
???????類中需要提供一個對應(yīng)參數(shù)列表的構(gòu)造函數(shù)。
????涉及的標(biāo)簽:
???????constructor-arg
?????????屬性:
??????????index:指定參數(shù)在構(gòu)造函數(shù)參數(shù)列表的索引位置
??????????type:指定參數(shù)在構(gòu)造函數(shù)中的數(shù)據(jù)類型
??????????name:指定參數(shù)在構(gòu)造函數(shù)中的名稱??,用這個找給誰賦值
??????????=======上面三個都是找給誰賦值,下面兩個指的是賦什么值的==============
??????????value:它能賦的值是基本數(shù)據(jù)類型和String類型
??????????ref:它能賦的值是其他bean類型,也就是說,必須得是在配置文件中配置過的bean
-->
<!--定義初始化一個Date的Bean-->
<bean id="date" class="java.util.Date"></bean>
name方式:
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl">
????<constructor-arg name="name" value="張三"></constructor-arg>
????<constructor-arg name="age" value="18"></constructor-arg>
????<constructor-arg name="birthday" ref="date"></constructor-arg>
</bean>
type方式:
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl">
??<constructor-arg type="java.lang.String" value="張三" />
??<constructor-arg type="java.lang.Integer" value="20" />
??<constructor-arg type="java.util.Date" ref="date" />
</bean>
index方式:
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl">
???<constructor-arg index="0" value="張三" />
???<constructor-arg index="1" value="20" />
???<constructor-arg index="2" ref="date" />
</bean>
顧名思義,就是在類中提供需要注入成員的set方法。具體代碼如下:
/** */
public class AccountServiceImpl implements AccountService {
????private String name;
????private Integer age;
????private Date birthday;
????public void setName(String name) {
???????this.name = name;
????}
????public void setAge(Integer age) {
???????this.age = age;
????}
????public void setBirthday(Date birthday) {
???????this.birthday = birthday;
????}
????@Override
????public void saveAccount() {
???????System.out.println(name+","+age+","+birthday);????
????}
}
<!-- 通過配置文件給bean中的屬性傳值:使用set方法的方式
????涉及的標(biāo)簽:
???????property
???????屬性:
?????????name:找的是類中set方法后面的部分
?????????ref:給屬性賦值是其他bean類型的
?????????value:給屬性賦值是基本數(shù)據(jù)類型和string類型的
????實(shí)際開發(fā)中,此種方式用的較多。
-->
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl">
???????<property name="name" value="test"></property>
???????<property name="age" value="21"></property>
???????<property name="birthday" ref="date"></property>
</bean>
<bean id="date" class="java.util.Date"></bean>
注入集合屬性
<!-- 注入集合數(shù)據(jù)
?????List結(jié)構(gòu)的:
???????array,list,set
????Map結(jié)構(gòu)的
???????map,entry,props,prop
-->
<bean id="accountService" class="com.oppo.service.impl.AccountServiceImpl">
????<!-- 在注入集合數(shù)據(jù)時,只要結(jié)構(gòu)相同,標(biāo)簽可以互換 -->
????<!-- 給數(shù)組注入數(shù)據(jù) -->
????<property name="myStrs">
???????<set>
?????????<value>AAA</value>
?????????<value>BBB</value>
?????????<value>CCC</value>
???????</set>
????</property>
????<!-- 注入list集合數(shù)據(jù) -->
????<property name="myList">
???????<array>
?????????<value>AAA</value>
?????????<value>BBB</value>
?????????<value>CCC</value>
???????</array>
????</property>
????<!-- 注入set集合數(shù)據(jù) -->
????<property name="mySet">
???????<list>
?????????<value>AAA</value>
?????????<value>BBB</value>
?????????<value>CCC</value>
???????</list>
????</property>
????<!-- 注入Map數(shù)據(jù) -->
????<property name="myMap">
???????<props>
?????????<prop key="testA">aaa</prop>
?????????<prop key="testB">bbb</prop>
???????</props>
????</property>
????<!-- 注入properties數(shù)據(jù) -->
????<property name="myProps">
???????<map>
?????????<entry key="testA" value="aaa"></entry>
?????????<entry key="testB">
??????????<value>bbb</value>
?????????</entry>
???????</map>
????</property>
</bean>