日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

spring读取properties配置文件_Spring-1

發布時間:2025/3/12 javascript 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring读取properties配置文件_Spring-1 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring共四天 第一天:spring框架的概述以及spring中基于XML的IOC配置 第二天:spring中基于注解的IOC和ioc的案例 第三天:spring中的aop和基于XML以及注解的AOP配置 第四天:spring中的JdbcTemlate以及Spring事務控制 ----------------------------------------------------- 1、spring的概述spring是什么spring的兩大核心spring的發展歷程和優勢spring體系結構 2、程序的耦合及解耦曾經案例中問題工廠模式解耦 3、IOC概念和spring中的IOCspring中基于XML的IOC環境搭建 4、依賴注入(Dependency Injection)

1 Spring第一天

1.1 程序耦合問題

package com.iteima.jdbc;import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet;/*** 程序的耦合* 耦合:程序間的依賴關系* 包括:* 類之間的依賴* 方法間的依賴* 解耦:* 降低程序間的依賴關系* 實際開發中:* 應該做到:編譯期不依賴,運行時才依賴* 解耦的思路:* 第一步:使用反射來創建對象,而避免使用new關鍵字* 第二步:通過讀取配置文件來獲取要創建的對象全限定類名*/ public class JdbcDemo1 {public static void main(String[] args) throws Exception {//1.注冊驅動 // DriverManager.registerDriver(new com.mysql.jdbc.Driver());Class.forName("com.mysql.jdbc.Driver");//2.獲取連接Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/eesy", "root", "1234");//3.獲取操作數據庫的預處理對象PreparedStatement pstm = conn.prepareStatement("select * from account");//4.執行SQL,得到結果集ResultSet rs = pstm.executeQuery();//5.遍歷結果集while(rs.next()) {System.out.println(rs.getString("name"));}//6.釋放資源rs.close();pstm.close();conn.close();} }

1.2 解耦

  • IAccountDao.java;
package com.itheima.dao;/*** 賬戶的持久層接口*/ public interface IAccountDao {/*** 模擬保存賬戶*/void saveAccount(); }
  • AccountDaoImpl.java;
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao;/*** 賬戶的持久層實現類*/ public class AccountDaoImpl implements IAccountDao {public void saveAccount() {System.out.println("保存了賬戶");} }
  • IAccountService.java;
package com.itheima.service;/*** 賬戶業務層的接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount(); }
  • AccountServiceImpl.java;
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.factory.BeanFactory; import com.itheima.service.IAccountService;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService { // private IAccountDao accountDao = new AccountDaoImpl();private IAccountDao accountDao = (IAccountDao)BeanFactory.getBean("accountDao"); // private int i = 1;public void saveAccount() {int i = 1;accountDao.saveAccount();System.out.println(i);i++;} }
  • Client.java;
package com.itheima.ui;import com.itheima.factory.BeanFactory; import com.itheima.service.IAccountService;/*** 模擬一個表現層,用于調用業務層*/ public class Client {public static void main(String[] args) {//IAccountService as = new AccountServiceImpl();for(int i = 0; i < 5; i++) {IAccountService as = (IAccountService) BeanFactory.getBean("accountService");System.out.println(as);as.saveAccount();}} }
  • BeanFactory.java;
package com.itheima.factory;import java.io.InputStream; import java.util.Enumeration; import java.util.HashMap; import java.util.Map; import java.util.Properties;/*** 一個創建Bean對象的工廠* Bean:在計算機英語中,有可重用組件的含義* JavaBean:用java語言編寫的可重用組件* javabean > 實體類* 它就是創建我們的service和dao對象的* 第一個:需要一個配置文件來配置我們的service和dao* 配置的內容:唯一標識=全限定類名(key=value)* 第二個:通過讀取配置文件中配置的內容,反射創建對象* 我的配置文件可以是xml也可以是properties*/ public class BeanFactory {//定義一個Properties對象private static Properties props;//定義一個Map,用于存放我們要創建的對象,我們把它稱之為容器private static Map<String, Object> beans;//使用靜態代碼塊為Properties對象賦值static {try {//實例化對象props = new Properties();//獲取properties文件的流對象InputStream in = BeanFactory.class.getClassLoader().getResourceAsStream("bean.properties");props.load(in);//實例化容器beans = new HashMap<String, Object>();//取出配置文件中所有的KeyEnumeration keys = props.keys();//遍歷枚舉while (keys.hasMoreElements()) {//取出每個KeyString key = keys.nextElement().toString();//根據key獲取valueString beanPath = props.getProperty(key);//反射創建對象Object value = Class.forName(beanPath).newInstance();//把key和value存入容器中beans.put(key, value);}} catch (Exception e) {throw new ExceptionInInitializerError("初始化properties失敗!");}}/*** 根據bean的名稱獲取對象*/public static Object getBean(String beanName) {return beans.get(beanName);}/*** 根據Bean的名稱獲取bean對象public static Object getBean(String beanName) {Object bean = null;try {String beanPath = props.getProperty(beanName); // System.out.println(beanPath);bean = Class.forName(beanPath).newInstance();//每次都會調用默認構造函數創建對象} catch (Exception e) {e.printStackTrace();}return bean;}*/ }
  • Bean.properties;
accountService=com.itheima.service.impl.AccountServiceImpl accountDao=com.itheima.dao.impl.AccountDaoImpl
  • 單例與多例;

1.3 IOC

  • IAccountDao.java;
package com.itheima.dao;/*** 賬戶的持久層接口*/ public interface IAccountDao {/*** 模擬保存賬戶*/void saveAccount(); }
  • AccountDaoImpl.java;
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao;/*** 賬戶的持久層實現類*/ public class AccountDaoImpl implements IAccountDao {public void saveAccount(){System.out.println("保存了賬戶");} }
  • IAccountService.java;
package com.itheima.service;/*** 賬戶業務層的接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount(); }
  • AccountServiceImpl.java;
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.service.IAccountService;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService {private IAccountDao accountDao ;public AccountServiceImpl(){System.out.println("對象創建了");}public void saveAccount(){accountDao.saveAccount();} }
  • Client.java;
package com.itheima.ui;import com.itheima.dao.IAccountDao; import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個表現層,用于調用業務層*/ public class Client {/*** 獲取spring的Ioc核心容器,并根據id獲取對象* ApplicationContext的三個常用實現類:* ClassPathXmlApplicationContext:它可以加載類路徑下的配置文件,要求配置文件必須在類路徑下,不在的話,加載不了,(更常用)* FileSystemXmlApplicationContext:它可以加載磁盤任意路徑下的配置文件(必須有訪問權限)* AnnotationConfigApplicationContext:它是用于讀取注解創建容器的,是明天的內容* 核心容器的兩個接口引發出的問題:* ApplicationContext: 單例對象適用 采用此接口* 它在構建核心容器時,創建對象采取的策略是采用立即加載的方式,也就是說,只要一讀取完配置文件馬上就創建配置文件中配置的對象* BeanFactory: 多例對象使用* 它在構建核心容器時,創建對象采取的策略是采用延遲加載的方式,也就是說,什么時候根據id獲取對象了,什么時候才真正的創建對象*/public static void main(String[] args) {//1.獲取核心容器對象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml"); // ApplicationContext ac = new FileSystemXmlApplicationContext("C:UserszhyDesktopbean.xml");//2.根據id獲取Bean對象IAccountService as = (IAccountService)ac.getBean("accountService");IAccountDao adao = ac.getBean("accountDao",IAccountDao.class);System.out.println(as);System.out.println(adao);as.saveAccount();//--------BeanFactory---------- // Resource resource = new ClassPathResource("bean.xml"); // BeanFactory factory = new XmlBeanFactory(resource); // IAccountService as = (IAccountService)factory.getBean("accountService"); // System.out.println(as);} }
  • bean.xml;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--把對象的創建交給spring來管理--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"></bean> </beans>

1.4 Spring三種創建Bean的方式

  • IAccountService.java;
package com.itheima.service;/*** 賬戶業務層的接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount(); }
  • AccountServiceImpl.java;
package com.itheima.service.impl;import com.itheima.service.IAccountService;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService {public AccountServiceImpl() {System.out.println("對象創建了");}public void saveAccount() {System.out.println("service中的saveAccount方法執行了。。。");}public void init() {System.out.println("對象初始化了。。。");}public void destroy() {System.out.println("對象銷毀了。。。");} }
  • StaticFactory.java;
package com.itheima.factory;import com.itheima.service.IAccountService; import com.itheima.service.impl.AccountServiceImpl;/*** 模擬一個工廠類(該類可能是存在于jar包中的,我們無法通過修改源碼的方式來提供默認構造函數)*/ public class StaticFactory {public static IAccountService getAccountService() {return new AccountServiceImpl();} }
  • InstanceFactory.java;
package com.itheima.factory;import com.itheima.service.IAccountService; import com.itheima.service.impl.AccountServiceImpl;/*** 模擬一個工廠類(該類可能是存在于jar包中的,我們無法通過修改源碼的方式來提供默認構造函數)*/ public class InstanceFactory {public IAccountService getAccountService() {return new AccountServiceImpl();} }
  • Client.java;
package com.itheima.ui;import com.itheima.service.IAccountService; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個表現層,用于調用業務層*/ public class Client {public static void main(String[] args) {//1.獲取核心容器對象 // ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根據id獲取Bean對象IAccountService as = (IAccountService)ac.getBean("accountService");as.saveAccount();//手動關閉容器ac.close();} }
  • bean.xml;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!--把對象的創建交給spring來管理--><!--spring對bean的管理細節1.創建bean的三種方式2.bean對象的作用范圍3.bean對象的生命周期--><!--創建Bean的三種方式 --><!-- 第一種方式:使用默認構造函數創建。在spring的配置文件中使用bean標簽,配以id和class屬性之后,且沒有其他屬性和標簽時。采用的就是默認構造函數創建bean對象,此時如果類中沒有默認構造函數,則對象無法創建。<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"></bean>--><!-- 第二種方式: 使用普通工廠中的方法創建對象(使用某個類中的方法創建對象,并存入spring容器)<bean id="instanceFactory" class="com.itheima.factory.InstanceFactory"></bean><bean id="accountService" factory-bean="instanceFactory" factory-method="getAccountService"></bean>--><!-- 第三種方式:使用工廠中的靜態方法創建對象(使用某個類中的靜態方法創建對象,并存入spring容器)<bean id="accountService" class="com.itheima.factory.StaticFactory" factory-method="getAccountService"></bean>--><!-- bean的作用范圍調整bean標簽的scope屬性:作用:用于指定bean的作用范圍取值:常用的就是單例的和多例的singleton:單例的(默認值)prototype:多例的request:作用于web應用的請求范圍session:作用于web應用的會話范圍global-session:作用于集群環境的會話范圍(全局會話范圍),當不是集群環境時,它就是session<bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl" scope="prototype"></bean>--><!-- bean對象的生命周期單例對象出生:當容器創建時對象出生活著:只要容器還在,對象一直活著死亡:容器銷毀,對象消亡總結:單例對象的生命周期和容器相同多例對象出生:當我們使用對象時spring框架為我們創建活著:對象只要是在使用過程中就一直活著。死亡:當對象長時間不用,且沒有別的對象引用時,由Java的垃圾回收器回收--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"scope="prototype" init-method="init" destroy-method="destroy"></bean> </beans>

1.5 Spring依賴注入

  • AccountServiceImpl.java;
package com.itheima.service.impl;import com.itheima.service.IAccountService; import java.util.Date;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService {//如果是經常變化的數據,并不適用于注入的方式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;}public void saveAccount() {System.out.println("service中的saveAccount方法執行了。。。" + name + "," + age + "," + birthday);} }
  • AccountServiceImpl2.java;
package com.itheima.service.impl;import com.itheima.service.IAccountService; import java.util.Date;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl2 implements IAccountService {//如果是經常變化的數據,并不適用于注入的方式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;}public void saveAccount() {System.out.println("service中的saveAccount方法執行了。。。" + name + "," + age + "," + birthday);} }
  • AccountServiceImpl3.java;
package com.itheima.service.impl;import com.itheima.service.IAccountService; import java.util.Arrays; import java.util.List; import java.util.Properties; import java.util.Set; import java.util.Map;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl3 implements IAccountService {private String[] myStrs;private List<String> myList;private Set<String> mySet;private Map<String, String> myMap;private Properties myProps;public void setMyStrs(String[] myStrs) {this.myStrs = myStrs;}public void setMyList(List<String> myList) {this.myList = myList;}public void setMySet(Set<String> mySet) {this.mySet = mySet;}public void setMyMap(Map<String, String> myMap) {this.myMap = myMap;}public void setMyProps(Properties myProps) {this.myProps = myProps;}public void saveAccount(){System.out.println(Arrays.toString(myStrs));System.out.println(myList);System.out.println(mySet);System.out.println(myMap);System.out.println(myProps);} }
  • IAccountService.java;
package com.itheima.service;/*** 賬戶業務層的接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount(); }
  • Client.java;
package com.itheima.ui;import com.itheima.service.IAccountService; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext;/*** 模擬一個表現層,用于調用業務層*/ public class Client {public static void main(String[] args) {//1.獲取核心容器對象ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.根據id獲取Bean對象 // IAccountService as = (IAccountService)ac.getBean("accountService"); // as.saveAccount(); // IAccountService as = (IAccountService)ac.getBean("accountService2"); // as.saveAccount();IAccountService as = (IAccountService)ac.getBean("accountService3");as.saveAccount();} }
  • bean.xml;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- spring中的依賴注入依賴注入:Dependency InjectionIOC的作用:降低程序間的耦合(依賴關系)依賴關系的管理:以后都交給spring來維護在當前類需要用到其他類的對象,由spring為我們提供,我們只需要在配置文件中說明依賴關系的維護:就稱之為依賴注入依賴注入:能注入的數據:有三類基本類型和String其他bean類型(在配置文件中或者注解配置過的bean)復雜類型/集合類型注入的方式:有三種第一種:使用構造函數提供第二種:使用set方法提供第三種:使用注解提供(明天的內容)--><!--構造函數注入:使用的標簽:constructor-arg標簽出現的位置:bean標簽的內部標簽中的屬性type:用于指定要注入的數據的數據類型,該數據類型也是構造函數中某個或某些參數的類型index:用于指定要注入的數據給構造函數中指定索引位置的參數賦值。索引的位置是從0開始name:用于指定給構造函數中指定名稱的參數賦值 常用的=============以上三個用于指定給構造函數中哪個參數賦值===============================value:用于提供基本類型和String類型的數據ref:用于指定其他的bean類型數據,它指的就是在spring的Ioc核心容器中出現過的bean對象優勢:在獲取bean對象時,注入數據是必須的操作,否則對象無法創建成功弊端:改變了bean對象的實例化方式,使我們在創建對象時,如果用不到這些數據,也必須提供--><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><constructor-arg name="name" value="泰斯特"></constructor-arg><constructor-arg name="age" value="18"></constructor-arg><constructor-arg name="birthday" ref="now"></constructor-arg></bean><!-- 配置一個日期對象 --><bean id="now" class="java.util.Date"></bean><!-- set方法注入 更常用的方式涉及的標簽:property出現的位置:bean標簽的內部標簽的屬性name:用于指定注入時所調用的set方法名稱value:用于提供基本類型和String類型的數據ref:用于指定其他的bean類型數據。它指的就是在spring的Ioc核心容器中出現過的bean對象優勢:創建對象時沒有明確的限制,可以直接使用默認構造函數弊端:如果有某個成員必須有值,則獲取對象是有可能set方法沒有執行--><bean id="accountService2" class="com.itheima.service.impl.AccountServiceImpl2"><property name="name" value="TEST" ></property><property name="age" value="21"></property><property name="birthday" ref="now"></property></bean><!-- 復雜類型的注入/集合類型的注入用于給List結構集合注入的標簽:list array set用于個Map結構集合注入的標簽:map props結構相同,標簽可以互換--><bean id="accountService3" class="com.itheima.service.impl.AccountServiceImpl3"><property name="myStrs"><set><value>AAA</value><value>BBB</value><value>CCC</value></set></property><property name="myList"><array><value>AAA</value><value>BBB</value><value>CCC</value></array></property><property name="mySet"><list><value>AAA</value><value>BBB</value><value>CCC</value></list></property><property name="myMap"><props><prop key="testC">ccc</prop><prop key="testD">ddd</prop></props></property><property name="myProps"><map><entry key="testA" value="aaa"></entry><entry key="testB"><value>BBB</value></entry></map></property></bean> </beans>spring第二天:spring基于注解的IOC以及IoC的案例 1、spring中ioc的常用注解 2、案例使用xml方式和注解方式實現單表的CRUD操作持久層技術選擇:dbutils 3、改造基于注解的ioc案例,使用純注解的方式實現spring的一些新注解使用 4、spring和Junit整合

2 第二天

2.1 注解IOC

  • IAccountDao.java;
package com.itheima.dao;/*** 賬戶的持久層接口*/ public interface IAccountDao {/*** 模擬保存賬戶*/void saveAccount(); }
  • AccountDaoImpl.java;
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao; import org.springframework.stereotype.Repository;/*** 賬戶的持久層實現類*/ @Repository("accountDao1") public class AccountDaoImpl implements IAccountDao {public void saveAccount() {System.out.println("保存了賬戶1111111111111");} }
  • AccountDaoImpl2.java;
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao; import org.springframework.stereotype.Repository;/*** 賬戶的持久層實現類*/ @Repository("accountDao2") public class AccountDaoImpl2 implements IAccountDao {public void saveAccount() {System.out.println("保存了賬戶2222222222222");} }
  • IAccountService.java;
package com.itheima.service;/*** 賬戶業務層的接口*/ public interface IAccountService {/*** 模擬保存賬戶*/void saveAccount(); }
  • AccountServiceImpl.java;
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.service.IAccountService; import org.springframework.stereotype.Service; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; import javax.annotation.Resource;/*** 賬戶的業務層實現類* 曾經XML的配置:* <bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"* scope="" init-method="" destroy-method="">* <property name="" value="" | ref=""></property>* </bean>** 用于創建對象的* 他們的作用就和在XML配置文件中編寫一個<bean>標簽實現的功能是一樣的* Component:* 作用:用于把當前類對象存入spring容器中* 屬性:* value:用于指定bean的id,當我們不寫時,它的默認值是當前類名,且首字母改小寫* Controller:一般用在表現層* Service:一般用在業務層* Repository:一般用在持久層* 以上三個注解他們的作用和屬性與Component是一模一樣* 他們三個是spring框架為我們提供明確的三層使用的注解,使我們的三層對象更加清晰** 用于注入數據的* 他們的作用就和在xml配置文件中的bean標簽中寫一個<property>標簽的作用是一樣的* Autowired:* 作用:自動按照類型注入。只要容器中有唯一的一個bean對象類型和要注入的變量類型匹配,就可以注入成功* 如果ioc容器中沒有任何bean的類型和要注入的變量類型匹配,則報錯* 如果Ioc容器中有多個類型匹配時:* 出現位置:* 可以是變量上,也可以是方法上* 細節:* 在使用注解注入時,set方法就不是必須的了* Qualifier:* 作用:在按照類中注入的基礎之上再按照名稱注入,它在給類成員注入時不能單獨使用。但是在給方法參數注入時可以(稍后我們講)* 屬性:* value:用于指定注入bean的id* Resource* 作用:直接按照bean的id注入,它可以獨立使用* 屬性:* name:用于指定bean的id* 以上三個注入都只能注入其他bean類型的數據,而基本類型和String類型無法使用上述注解實現* 另外,集合類型的注入只能通過XML來實現** Value* 作用:用于注入基本類型和String類型的數據* 屬性:* value:用于指定數據的值,它可以使用spring中SpEL(也就是spring的el表達式)* SpEL的寫法:${表達式}** 用于改變作用范圍的* 他們的作用就和在bean標簽中使用scope屬性實現的功能是一樣的* Scope* 作用:用于指定bean的作用范圍* 屬性:* value:指定范圍的取值,常用取值:singleton prototype** 和生命周期相關 了解* 他們的作用就和在bean標簽中使用init-method和destroy-methode的作用是一樣的* PreDestroy* 作用:用于指定銷毀方法* PostConstruct* 作用:用于指定初始化方法*/ @Service("accountService") //@Scope("prototype") public class AccountServiceImpl implements IAccountService {// @Autowired // @Qualifier("accountDao1")@Resource(name = "accountDao2")private IAccountDao accountDao = null;@PostConstructpublic void init() {System.out.println("初始化方法執行了");}@PreDestroypublic void destroy() {System.out.println("銷毀方法執行了");}public void saveAccount() {accountDao.saveAccount();} }
  • bean.xml;
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!--告知spring在創建容器時要掃描的包,配置所需要的標簽不是在beans的約束中,而是一個名稱為context名稱空間和約束中--><context:component-scan base-package="com.itheima"></context:component-scan> </beans>

2.2 基于XML的IOC案例

  • Account.java;
package com.itheima.domain;import java.io.Serializable;/*** 賬戶的實體類*/ public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + ''' +", money=" + money +'}';} }
  • IAccountDao.java;
package com.itheima.dao;import com.itheima.domain.Account; import java.util.List;/*** 賬戶的持久層接口*/ public interface IAccountDao {List<Account> findAllAccount();Account findAccountById(Integer accountId);void saveAccount(Account account);void updateAccount(Account account);void deleteAccount(Integer acccountId); }
  • AccountDaoImpl.java;
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import java.util.List;/*** 賬戶的持久層實現類*/ public class AccountDaoImpl implements IAccountDao {private QueryRunner runner;public void setRunner(QueryRunner runner) {this.runner = runner;}@Overridepublic List<Account> findAllAccount() {try{return runner.query("select * from account", new BeanListHandler<Account>(Account.class));}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic Account findAccountById(Integer accountId) {try{return runner.query("select * from account where id = ? ", new BeanHandler<Account>(Account.class),accountId);}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void saveAccount(Account account) {try{runner.update("insert into account(name,money)values(?,?)", account.getName(), account.getMoney());}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void updateAccount(Account account) {try{runner.update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void deleteAccount(Integer accountId) {try{runner.update("delete from account where id=?", accountId);}catch (Exception e) {throw new RuntimeException(e);}} }
  • IAccountService.java;
package com.itheima.service;import com.itheima.domain.Account; import java.util.List;/*** 賬戶的業務層接口*/ public interface IAccountService {List<Account> findAllAccount();Account findAccountById(Integer accountId);void saveAccount(Account account);void updateAccount(Account account);void deleteAccount(Integer acccountId); }
  • AccountServiceImpl.java;
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import com.itheima.service.IAccountService; import java.util.List;/*** 賬戶的業務層實現類*/ public class AccountServiceImpl implements IAccountService{private IAccountDao accountDao;public void setAccountDao(IAccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic List<Account> findAllAccount() {return accountDao.findAllAccount();}@Overridepublic Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}@Overridepublic void saveAccount(Account account) {accountDao.saveAccount(account);}@Overridepublic void updateAccount(Account account) {accountDao.updateAccount(account);}@Overridepublic void deleteAccount(Integer acccountId) {accountDao.deleteAccount(acccountId);} }
  • bean.xml;
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd"><!-- 配置Service --><bean id="accountService" class="com.itheima.service.impl.AccountServiceImpl"><!-- 注入dao --><property name="accountDao" ref="accountDao"></property></bean><!--配置Dao對象--><bean id="accountDao" class="com.itheima.dao.impl.AccountDaoImpl"><!-- 注入QueryRunner --><property name="runner" ref="runner"></property></bean><!--配置QueryRunner--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"><!--注入數據源--><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!-- 配置數據源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--連接數據庫的必備信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="1234"></property></bean> </beans>
  • AccountServiceTest.java;
package com.itheima.test;import com.itheima.domain.Account; import com.itheima.service.IAccountService; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List;/*** 使用Junit單元測試:測試我們的配置*/ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:bean.xml") public class AccountServiceTest {@Autowiredprivate IAccountService as;@Testpublic void testFindAll() {//3.執行方法List<Account> accounts = as.findAllAccount();for(Account account : accounts) {System.out.println(account);}}@Testpublic void testFindOne() {//3.執行方法Account account = as.findAccountById(1);System.out.println(account);}@Testpublic void testSave() {Account account = new Account();account.setName("test");account.setMoney(12345f);//3.執行方法as.saveAccount(account);}@Testpublic void testUpdate() {//3.執行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//3.執行方法as.deleteAccount(4);} }

2.3 基于注解的IOC

  • Account.java;
package com.itheima.domain;import java.io.Serializable;/*** 賬戶的實體類*/ public class Account implements Serializable {private Integer id;private String name;private Float money;public Integer getId() {return id;}public void setId(Integer id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public Float getMoney() {return money;}public void setMoney(Float money) {this.money = money;}@Overridepublic String toString() {return "Account{" +"id=" + id +", name='" + name + ''' +", money=" + money +'}';} }
  • IAccountDao.java;
package com.itheima.dao;import com.itheima.domain.Account; import java.util.List;/*** 賬戶的持久層接口*/ public interface IAccountDao {List<Account> findAllAccount();Account findAccountById(Integer accountId);void saveAccount(Account account);void updateAccount(Account account);void deleteAccount(Integer acccountId); }
  • AccountDaoImpl.java;
package com.itheima.dao.impl;import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import java.util.List;/*** 賬戶的持久層實現類*/ @Repository("accountDao") public class AccountDaoImpl implements IAccountDao {@Autowiredprivate QueryRunner runner;@Overridepublic List<Account> findAllAccount() {try{return runner.query("select * from account", new BeanListHandler<Account>(Account.class));}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic Account findAccountById(Integer accountId) {try{return runner.query("select * from account where id = ? ", new BeanHandler<Account>(Account.class), accountId);}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void saveAccount(Account account) {try{runner.update("insert into account(name,money)values(?,?)", account.getName(), account.getMoney());}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void updateAccount(Account account) {try{runner.update("update account set name=?,money=? where id=?", account.getName(), account.getMoney(), account.getId());}catch (Exception e) {throw new RuntimeException(e);}}@Overridepublic void deleteAccount(Integer accountId) {try{runner.update("delete from account where id=?", accountId);}catch (Exception e) {throw new RuntimeException(e);}} }
  • IAccountService.java;
package com.itheima.service;import com.itheima.domain.Account; import java.util.List;/*** 賬戶的業務層接口*/ public interface IAccountService {List<Account> findAllAccount();Account findAccountById(Integer accountId);void saveAccount(Account account);void updateAccount(Account account);void deleteAccount(Integer acccountId); }
  • AccountServiceImpl.java;
package com.itheima.service.impl;import com.itheima.dao.IAccountDao; import com.itheima.domain.Account; import com.itheima.service.IAccountService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List;/*** 賬戶的業務層實現類*/ @Service("accountService") public class AccountServiceImpl implements IAccountService {@Autowiredprivate IAccountDao accountDao;@Overridepublic List<Account> findAllAccount() {return accountDao.findAllAccount();}@Overridepublic Account findAccountById(Integer accountId) {return accountDao.findAccountById(accountId);}@Overridepublic void saveAccount(Account account) {accountDao.saveAccount(account);}@Overridepublic void updateAccount(Account account) {accountDao.updateAccount(account);}@Overridepublic void deleteAccount(Integer acccountId) {accountDao.deleteAccount(acccountId);} }
  • bean.xml;
<?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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><!-- 告知spring在創建容器時要掃描的包 --><context:component-scan base-package="com.itheima"></context:component-scan><!--配置QueryRunner--><bean id="runner" class="org.apache.commons.dbutils.QueryRunner" scope="prototype"><!--注入數據源--><constructor-arg name="ds" ref="dataSource"></constructor-arg></bean><!-- 配置數據源 --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><!--連接數據庫的必備信息--><property name="driverClass" value="com.mysql.jdbc.Driver"></property><property name="jdbcUrl" value="jdbc:mysql://localhost:3306/eesy"></property><property name="user" value="root"></property><property name="password" value="1234"></property></bean> </beans>
  • AccountServiceTest.java;
package com.itheima.test;import com.itheima.domain.Account; import com.itheima.service.IAccountService; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import java.util.List;/*** 使用Junit單元測試:測試我們的配置*/ public class AccountServiceTest {@Testpublic void testFindAll() {//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業務層對象IAccountService as = ac.getBean("accountService", IAccountService.class);//3.執行方法List<Account> accounts = as.findAllAccount();for(Account account : accounts){System.out.println(account);}}@Testpublic void testFindOne() {//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業務層對象IAccountService as = ac.getBean("accountService", IAccountService.class);//3.執行方法Account account = as.findAccountById(1);System.out.println(account);}@Testpublic void testSave() {Account account = new Account();account.setName("test");account.setMoney(12345f);//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業務層對象IAccountService as = ac.getBean("accountService", IAccountService.class);//3.執行方法as.saveAccount(account);}@Testpublic void testUpdate() {//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業務層對象IAccountService as = ac.getBean("accountService", IAccountService.class);//3.執行方法Account account = as.findAccountById(4);account.setMoney(23456f);as.updateAccount(account);}@Testpublic void testDelete() {//1.獲取容器ApplicationContext ac = new ClassPathXmlApplicationContext("bean.xml");//2.得到業務層對象IAccountService as = ac.getBean("accountService", IAccountService.class);//3.執行方法as.deleteAccount(4);} }

參考

Spring教程IDEA版-4天-2018黑馬SSM-02_嗶哩嗶哩 (゜-゜)つロ 干杯~-bilibili?www.bilibili.com

總結

以上是生活随笔為你收集整理的spring读取properties配置文件_Spring-1的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩综合在线视频 | 国产在线播放一区二区三区 | 爱情岛论坛自拍亚洲品质极速最新章 | 午夜精品久久久 | 一级特毛片 | 免费99精品国产自在在线 | 欧美日韩中文字幕视频 | 日本黄色高清视频 | 国产又粗又猛又爽又黄的 | 激情福利网 | 网站一级片 | 淫片aaa| 日韩女同强女同hd | 最近中文字幕在线免费观看 | 久久97精品久久久久久久不卡 | 69网址| 免费视频福利 | 操操色| 小镇姑娘1979版 | 91免费黄 | 蜜桃av在线看 | 成人国产免费 | 免费的毛片网站 | 天天综合天天综合 | 国产无遮无挡120秒 欧美综合图片 | 一级片在线播放 | 国产精品自拍在线 | 丁香婷婷一区二区三区 | av免费入口 | 免费无码不卡视频在线观看 | 天堂中文字幕在线观看 | 亚洲熟妇无码av在线播放 | 少妇第一次交换又紧又爽 | 久久在线免费观看视频 | 欧美色婷婷| 筱田优全部av免费观看 | 欧美午夜视频 | 性做久久久久久久久 | a√在线观看 | 成年人免费视频观看 | 91在线观看免费高清完整版在线观看 | 人妻中文字幕一区二区三区 | 亚洲欧洲在线播放 | 亚洲男人第一网站 | 日韩在线不卡av | 6699av| 碧蓝之海动漫在线观看免费高清 | 男女男精品视频网站 | 牛牛视频在线 | 亚洲精品一区在线 | 婷婷午夜影院 | 国产亚洲精品久久久久动 | 诱惑の诱惑筱田优在线播放 | 激情五月婷婷综合 | 又黄又爽视频在线观看 | 久啪视频 | 日本色悠悠| 国产成人精品免费在线观看 | 青青在线视频 | 五月天黄色小说 | 日本久久久久久久久久久 | 国产欧美一区二区三区视频在线观看 | 亚洲一级理论片 | 91九色精品| jizzzz中国 | 91久色视频 | 欧美一级一级 | 国产久视频 | 国产无遮挡裸体免费视频 | 日本久久久久久 | 91免费版黄| 亚洲精品6 | 在线视频h| 国产激情自拍 | a午夜| 国产级毛片 | 天天av天天操 | 一本一道精品欧美中文字幕 | 在线免费观看高清视频 | 一级黄片毛片 | 国产大片一区 | 麻豆视频网站在线观看 | 亚洲中出 | 日韩 欧美 精品 | 成人免费影院 | 国产xxxxxx| 琪琪五月天| 日韩中文字幕第一页 | 人人干视频 | 性感美女被爆操 | 中文字幕日产 | 91看片免费版 | 大陆农村乡下av | 99视频观看 | 微拍福利一区二区 | 欲涩漫入口免费网站 | 国产福利在线看 | 欧日韩不卡视频 | 亚洲一卡二卡在线观看 |