中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當(dāng)前位置: 首頁 > news >正文

畢業(yè)設(shè)計(jì)做健身房網(wǎng)站的意義網(wǎng)站seo方案案例

畢業(yè)設(shè)計(jì)做健身房網(wǎng)站的意義,網(wǎng)站seo方案案例,武漢光谷律師,做仿牌網(wǎng)站空間IOC概念和原理 IOC概念 IOC就是控制反射,把對象創(chuàng)建和對象之間的調(diào)用過程,交給Spring進(jìn)行管理使用IOC的目的:降低耦合度 IOC底層原理 xml解析、工廠模式、反射 圖解: 原始模式 耦合度太高了,即當(dāng)dao改了&#xf…

IOC概念和原理

IOC概念

  1. IOC就是控制反射,把對象創(chuàng)建和對象之間的調(diào)用過程,交給Spring進(jìn)行管理
  2. 使用IOC的目的:降低耦合度

IOC底層原理

  • xml解析、工廠模式、反射

圖解:

原始模式

img

  • 耦合度太高了,即當(dāng)dao改了,service也跟著要改
工廠模式

img

  • 工廠模式目的:使耦合度降低到最低限度,降低service和dao之間的聯(lián)系
IOC過程

img

IOC(BeanFactory、ApplicationContext接口)

  1. IOC思想基于IOC容器完成,IOC容器底層就是對象工廠,本質(zhì)上就是一個(gè)工廠

  2. Spring提供IOC容器實(shí)現(xiàn)兩種方式:(兩個(gè)接口

    1. BeanFactory:IOC容器基本實(shí)現(xiàn),是Spring內(nèi)部的使用接口,不提供開發(fā)人員進(jìn)行使用

特點(diǎn):加載配置文件的時(shí)候不會創(chuàng)建對象,在獲取對象(使用)才去創(chuàng)建對象

即BeanFactory在加載spring配置文件的時(shí)候,不會創(chuàng)建對象,只有在獲取對象要去使用的時(shí)候才會去創(chuàng)建對象

    1. ApplicationContext:BeanFactory接口的子接口,提供更多更強(qiáng)大的功能,一般由開發(fā)人員進(jìn)行使用

特點(diǎn):加載配置文件的時(shí)候就會把在配置文件的對象進(jìn)行創(chuàng)建

而ApplicationContext在加載spring配置文件的時(shí)候,就會創(chuàng)建對象

  1. ApplicationContext接口有實(shí)現(xiàn)類

img

  • FileSystemXmlApplicationContext這個(gè)實(shí)現(xiàn)類,里面?zhèn)魅氲氖?strong>xml文件的磁盤位置,比如:D:/java/bean1.xml
  • 而ClassPathXmlApplicationContext這個(gè)實(shí)現(xiàn)類,里面?zhèn)魅氲氖?strong>類路徑,即src下

img

IOC操作Bean管理

  • Bean管理指的是兩個(gè)操作:
  1. Spring創(chuàng)建對象
  2. Spring注入屬性
  • Bean管理操作有兩種方式:
  1. 基于xml配置文件方式實(shí)現(xiàn)
  2. 基于注解方式實(shí)現(xiàn)

💖IOC操作Bean管理(基于xml方式)

基于xml方式創(chuàng)建對象
<!--配置User類的對象的創(chuàng)建-->
<!-- id就是起個(gè)名字(別名),class中填入這個(gè)類的全路徑 -->
<bean id="user" class="com.zan.spring5.User"></bean>

做法:

  1. Spring配置文件中,使用bean標(biāo)簽,標(biāo)簽里面添加對應(yīng)屬性,就可以實(shí)現(xiàn)對象創(chuàng)建

  2. 在bean標(biāo)簽中有很多屬性,常用屬性

    1. id屬性:唯一標(biāo)識,也相當(dāng)于別名
    2. class屬性,類全路徑(即包名+類名
    3. name屬性:跟id屬性一樣,作為唯一標(biāo)識,但是用的很少,name的區(qū)別就是可以使用特殊符號,如:/
  3. 創(chuàng)建對象時(shí)候,默認(rèn)是執(zhí)行類的無參構(gòu)造器來完成對象的創(chuàng)建

  • 因此如果類中沒有無參構(gòu)造器,則會報(bào)錯(cuò),因?yàn)檎也坏?/li>

img

  • 沒有無參構(gòu)造器,報(bào)錯(cuò)

img

基于xml方式注入屬性
  • DI:依賴注入,就是注入屬性
使用set方法進(jìn)行注入屬性
  • 原始方法(以前的方法來注入屬性)

img

  • 現(xiàn)用Spring來進(jìn)行注入屬性
  1. 創(chuàng)建類,定義屬性和對應(yīng)的set方法
/*** 演示使用set方法進(jìn)行注入屬性*/
public class Book {//創(chuàng)建屬性private String bname;private String bauthor;//創(chuàng)建屬性對應(yīng)的set方法注入public void setBname(String bname) {this.bname = bname;}public void setBauthor(String bauthor) {this.bauthor = bauthor;}//測試方法,用來看是否注入成功public void testDemo() {System.out.println(bname + " : " + bauthor);}
}
  1. 在Spring的配置文件xml中配置對象創(chuàng)建,配置屬性注入
<!--set方法注入屬性-->
<bean id="book" class="com.zan.spring5.Book"><!--在bean標(biāo)簽里面使用property,用來完成屬性的注入name:類里面屬性名稱value:注入的屬性值--><property name="bname" value="abc"></property><property name="bauthor" value="zan哥"></property>
</bean>

img

  • 測試結(jié)果

img

使用有參構(gòu)造器注入屬性
  • 原始方法(以前的方法來注入屬性)

img

  • 現(xiàn)用Spring來進(jìn)行注入屬性
  1. 創(chuàng)建類,定義屬性,創(chuàng)建屬性對應(yīng)的有參構(gòu)造方法
package com.zan.spring5;/*** 使用有參構(gòu)造器注入屬性*/
public class Orders {private String oname;private String address;public Orders(String oname, String address) {this.oname = oname;this.address = address;}public void ordersTest() {System.out.println(oname + " : " + address);}
}
  1. 在Spring的配置文件xml中進(jìn)行配置
<!--用有參構(gòu)造器注入屬性-->
<bean id="order" class="com.zan.spring5.Orders"><!--        <constructor-arg name="oname" value="電腦"></constructor-arg>--><constructor-arg name="address" value="China"></constructor-arg><constructor-arg index="0" value="電腦1"></constructor-arg>
</bean>
  • 這個(gè)可以使用name來表示屬性的,也可以用index來表示屬性

img

  • 使用index來代表屬性

img

p名稱空間注入(了解)
  • 使用p名稱空間注入,可以簡化基于xml配置方式
  1. 添加 p 名稱空間在配置文件中
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  • beans標(biāo)簽的頭內(nèi)容其實(shí)是xml的相關(guān)約束
  1. 進(jìn)行屬性注入,在bean標(biāo)簽里面進(jìn)行操作
<bean id="book" class="com.zan.spring5.Book" p:bname="abcd" p:bauthor="Zan大哥"></bean>

img

基于xml方式注入屬性(其他類型屬性)
字面值
  1. null值
<!--設(shè)置一個(gè)null值,空值-->
<property name="address"><null/>
</property>

img

  1. 屬性值包含特殊符號

img

<!--屬性值中包含特殊符號,如 <>解決方法: 1. 將<> 進(jìn)行轉(zhuǎn)義,即&lt; &gt;2. 把帶特殊符號內(nèi)容寫到CDATA中
-->
<property name="address"><value><![CDATA[<<南京>>]]></value>
</property>
  • 這個(gè)格式快捷鍵 - CD

img

注入屬性 - 外部bean
  1. 創(chuàng)建兩個(gè)類service類和dao類
  2. 在service中調(diào)用dao里面的方法
public class UserDaoImpl implements UserDao{@Overridepublic void update() {System.out.println("dao update...........");}
}public class UserService {//創(chuàng)建UserDao類型屬性,生成set方法private UserDao userDao;public void setUserDao(UserDao userDao) {this.userDao = userDao;}public void add() {System.out.println("service add..........");userDao.update();//創(chuàng)建UserDao對象 - 原始方式//UserDao userDao = new UserDaoImpl();//userDao.update();}
}
  1. 在Spring配置文件中進(jìn)行配置
<!--1. service和dao對象創(chuàng)建-->
<bean id="userService" class="com.zan.spring5.service.UserService"><!--注入userDao對象name:類里面的屬性名稱ref:創(chuàng)建的userDao對象里面bean標(biāo)簽的id值--><property name="userDao" ref="userDaoImpl"></property>
</bean><bean id="userDaoImpl" class="com.zan.spring5.dao.UserDaoImpl"></bean>

img

注入屬性 - 內(nèi)部bean

一對多關(guān)系:部門和員工,一個(gè)部分有多個(gè)員工,一個(gè)員工屬于一個(gè)部分,部門是一,員工是多

  • 在實(shí)體類之間表示一對多關(guān)系,員工表示所屬部門,使用對象類型屬性進(jìn)行表示
//部門類
public class Dept {private String dname;public void setDname(String dname) {this.dname = dname;}
}
//員工類
public class Emp {private String ename;private String gender;//員工屬于某一個(gè)部門,使用對象形式表示private Dept dept;public void setDept(Dept dept) {this.dept = dept;}public void setEname(String ename) {this.ename = ename;}public void setGender(String gender) {this.gender = gender;}
}

在spring配置文件中進(jìn)行配置

<!--內(nèi)部bean-->
<bean id="emp" class="com.zan.spring5.bean.Emp"><!--先設(shè)置兩個(gè)普通的屬性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--設(shè)置對象類型屬性--><property name="dept"><bean id="dept" class="com.zan.spring5.bean.Dept"><property name="dname" value="安保部"></property></bean></property>
</bean>

img

注入屬性 - 級聯(lián)賦值
第一種寫法
<!--級聯(lián)賦值--><bean id="emp" class="com.zan.spring5.bean.Emp"><!--先設(shè)置兩個(gè)普通的屬性--><property name="ename" value="lucy"></property><property name="gender" value=""></property><!--級聯(lián)賦值--><property name="dept" ref="dept"></property></bean><bean id="dept" class="com.zan.spring5.bean.Dept"><property name="dname" value="財(cái)務(wù)部"></property></bean>

img

第二種寫法
  • 首先要先給dept這個(gè)屬性設(shè)置get方法
//生成dept的get方法
public Dept getDept() {return dept;
}
<!--級聯(lián)賦值--><bean id="emp" class="com.zan.spring5.bean.Emp"><!--先設(shè)置兩個(gè)普通的屬性--><property name="ename" value="lucy"></property><property name="gender" value="女"></property><!--級聯(lián)賦值--><property name="dept" ref="dept"></property><property name="dept.dname" value="教學(xué)部"></property></bean><bean id="dept" class="com.zan.spring5.bean.Dept"></bean>

img

注入集合屬性
  • 注入數(shù)組類型、List集合類型、Map集合類型、Set集合類型屬性
  1. 創(chuàng)建類,定義數(shù)組、List、Map、Set類型屬性,生成對應(yīng)的set方法
package com.zan.spring5.collectiontype;import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;public class Stu {//1. 數(shù)組類型屬性private String[] courses;//課程//2. List集合類型屬性private List<String> list;//3. Map集合類型屬性private Map<String, String> map;//4. set集合類型屬性private Set<String> sets;//學(xué)生所學(xué)多門課程private List<Course> courseList;public void setCourseList(List<Course> courseList) {this.courseList = courseList;}public void setCourses(String[] courses) {this.courses = courses;}public void setList(List<String> list) {this.list = list;}public void setMap(Map<String, String> map) {this.map = map;}public void setSets(Set<String> sets) {this.sets = sets;}//測試方法public void test() {System.out.println(Arrays.toString(courses));System.out.println(list);System.out.println(map);System.out.println(sets);System.out.println(courseList);}
}
  1. 在Spring配置文件xml中進(jìn)行配置
<!--集合類型屬性的注入-->
<bean id="stu" class="com.zan.spring5.collectiontype.Stu"><!--數(shù)組類型屬性注入--><property name="courses"><array><value>Java課程</value><value>C++課程</value></array></property><!--list類型屬性注入--><property name="list"><list><value>小三</value><value>張三</value></list></property><!--map類型屬性注入--><property name="map"><map><entry key="JAVA" value="java"/><entry key="JSP" value="jsp"/></map></property><!--set類型屬性注入--><property name="sets"><set><value>MySQL</value><value>Redis</value></set></property>
  • 數(shù)組類型可以用array標(biāo)簽,也可以用list標(biāo)簽

img

細(xì)節(jié)問題
  1. 在集合里面設(shè)置對象類型值
<!--創(chuàng)建多個(gè)course對象,將course對象傳過去即可-->
<bean id="course1" class="com.zan.spring5.collectiontype.Course"><property name="cname" value="Spring5框架課程"/>
</bean>
<bean id="course2" class="com.zan.spring5.collectiontype.Course"><property name="cname" value="MyBatis框架課程"/>
</bean><bean><!--注入list集合類型,值是對象--><property name="courseList"><list><ref bean="course1"/><ref bean="course2"/></list></property>
</bean>

img

img

  1. 把集合注入部分提取出來

(a)在Spring配置文件xml中引入名稱空間util

  • 在xml文件中的約束文件處引入名稱空間util
<?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:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">
</beans>

img

(b)使用util標(biāo)簽完成list集合注入提取

<!--提取list集合類型屬性的注入-->
<util:list id="bookList"><value>Java核心技術(shù)卷1</value><value>Java核心技術(shù)卷2</value><value>MySQL數(shù)據(jù)庫</value>
</util:list><!--提取list集合類型屬性注入使用-->
<bean id="book" class="com.zan.spring5.collectiontype.Book"><property name="list" ref="bookList"></property>
</bean>

img

IOC操作Bean管理(FactoryBean)

  • Spring有兩種類型bean,一種普通bean,另外一種工廠bean(FactoryBean)
  1. 普通bean:在配置文件中定義bean類型就是返回類型
  2. 工廠bean:在配置文件定義bean類型可以和返回類類型不一樣

實(shí)現(xiàn)步驟:

(a)創(chuàng)建類,讓這個(gè)類作為工廠bean,即****實(shí)現(xiàn)接口FactoryBean

(b)實(shí)現(xiàn)接口里面的方法,在實(shí)現(xiàn)的方法中定義返回的bean類型

package com.zan.spring5.factorybean;import com.zan.spring5.collectiontype.Course;
import org.springframework.beans.factory.FactoryBean;public class MyBean implements FactoryBean<Course> {//定義返回bean對象,即返回類型@Overridepublic Course getObject() throws Exception {Course course = new Course();course.setCname("abc");return course;}@Overridepublic Class<?> getObjectType() {return null;}@Overridepublic boolean isSingleton() {return FactoryBean.super.isSingleton();}
}
<bean id="myBean" class="com.zan.spring5.factorybean.MyBean"></bean>
  • 注意:這里的測試類的getBean里面獲取的類應(yīng)該是我們返回的類型,即是Course,不然會報(bào)類型不匹配錯(cuò)誤

img

img

IOC操作Bean管理(bean作用域)

  1. 在Spring中,我們可以設(shè)置bean示例是單實(shí)例或者多實(shí)例
  2. 在Spring中,默認(rèn)情況下,bean是單實(shí)例對象

單實(shí)例:地址一樣

多實(shí)例:地址不一樣

@Test
public void testBook() {ApplicationContext context = new ClassPathXmlApplicationContext("bean2.xml");Book book1 = context.getBean("book", Book.class);Book book2 = context.getBean("book", Book.class);System.out.println(book1);System.out.println(book2);}

img

  1. 設(shè)置單實(shí)例或多實(shí)例

    1. 在Spring配置文件中的bean標(biāo)簽里面有屬性(scope)用于設(shè)置單實(shí)例還是多實(shí)例
    2. scope:有兩個(gè)值,singleton單實(shí)例(默認(rèn)),prototype多實(shí)例
<!--提取list集合類型屬性的注入-->
<util:list id="bookList"><value>Java核心技術(shù)卷1</value><value>Java核心技術(shù)卷2</value><value>MySQL數(shù)據(jù)庫</value>
</util:list><!--提取list集合類型屬性注入使用-->
<bean id="book" class="com.zan.spring5.collectiontype.Book" scope="prototype"><property name="list" ref="bookList"></property>
</bean>

img

  • singleton和prototype區(qū)別
  1. singleton單實(shí)例,prototype多實(shí)例
  2. 設(shè)置scope值是singleton時(shí),測試文件在加載Spring配置文件的時(shí)候就會創(chuàng)建單實(shí)例對象,因此這時(shí)的地址就已經(jīng)確定,之后創(chuàng)建的對象都是同一個(gè)地址

設(shè)置scope值是prototype時(shí),測試文件不是在加載Spring配置文件的時(shí)候創(chuàng)建對象,而是在調(diào)用getBean方法的時(shí)候創(chuàng)建多實(shí)例對象

IOC操作Bean管理(bean生命周期)

生命周期:從對象創(chuàng)建到對象銷毀的過程

bean生命周期:

  1. 通過構(gòu)造器創(chuàng)建bean實(shí)例(無參數(shù)構(gòu)造器)
  2. 為bean的屬性設(shè)置值和對其他bean的引用(調(diào)用set方法)
  3. 調(diào)用bean的初始化的方法(需要進(jìn)行配置初始化的方法)
  4. bean可以使用了(對象獲取到了)
  5. 當(dāng)容器關(guān)閉的時(shí)候,調(diào)用bean的銷毀的方法(需要進(jìn)行配置銷毀的方法)
  • 注意:第三步初始化和第五步銷毀都要自己額外配置

演示bean的生命周期

package com.zan.spring5.bean;public class Orders {private String oname;public Orders() {System.out.println("第一步 執(zhí)行無參構(gòu)造器創(chuàng)建bean實(shí)例");}public void setOname(String oname) {this.oname = oname;System.out.println("第二步 調(diào)用set方法設(shè)置屬性值");}//創(chuàng)建初始化的方法public void init() {System.out.println("第三步 調(diào)用初始化的方法");}//創(chuàng)建銷毀的方法public void destroy() {System.out.println("第五步 調(diào)用銷毀的方法");}
}
  • 在bean標(biāo)簽中設(shè)置初始化init-method和銷毀destory-method
<bean id="orders" class="com.zan.spring5.bean.Orders" init-method="init" destroy-method="destroy"><property name="oname" value="手機(jī)"></property>
</bean>
  • 測試方法:
@Test
public void testBean3() {//ApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("bean4.xml");Orders orders = context.getBean("orders", Orders.class);System.out.println("第四步 獲取創(chuàng)建bean實(shí)例對象");System.out.println(orders);//手動讓bean銷毀,才會調(diào)用bean銷毀的方法,這個(gè)close是在ClassPathXmlApplicationContext這個(gè)實(shí)現(xiàn)類里面的//((ClassPathXmlApplicationContext)context).close();context.close();
}

img

其實(shí),bean的生命周期還有兩步,即七步

bean的后置處理器:

1)通過構(gòu)造器創(chuàng)建 bean 實(shí)例(無參數(shù)構(gòu)造)

2)為 bean 的屬性設(shè)置值和對其他 bean 引用(調(diào)用 set 方法)

3)把 bean 實(shí)例傳遞 bean 后置處理器的方法 postProcessBeforeInitialization

4)調(diào)用 bean 的初始化的方法(需要進(jìn)行配置初始化的方法)

5)把 bean 實(shí)例傳遞 bean 后置處理器的方法 postProcessAfterInitialization

6)bean 可以使用了(對象獲取到了)

7)當(dāng)容器關(guān)閉時(shí)候,調(diào)用 bean 的銷毀的方法(需要進(jìn)行配置銷毀的方法)

  • 即添加在初始化之前和初始化之后
  • 要想添加這兩步,需要有一個(gè)類去實(shí)現(xiàn)BeanPostProcessor接口
  1. 創(chuàng)建類,實(shí)現(xiàn)接口BeanPostProcessor,創(chuàng)建后置處理器
package com.zan.spring5.bean;import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;public class MyBeanPost implements BeanPostProcessor {@Overridepublic Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之前執(zhí)行的方法");return bean;}@Overridepublic Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {System.out.println("在初始化之后執(zhí)行的方法");return bean;}
}
  1. 配置后置處理器
<!--配置后置處理器對當(dāng)前的所有bean都添加后置處理器
-->
<bean id="myBeanPost" class="com.zan.spring5.bean.MyBeanPost"></bean>

img

IOC操作Bean管理(xml自動裝配)【用的很少,一般都用注解】

自動裝配:根據(jù)指定裝配規(guī)則(屬性名稱或者屬性類型),Spring自動將匹配的屬性值進(jìn)行注入【之前的指定name和value,是手動裝配】

package com.zan.spring5.autowire;public class Dept {@Overridepublic String toString() {return "Dept{}";}
}
package com.zan.spring5.autowire;public class Emp {private Dept dept;public void setDept(Dept dept) {this.dept = dept;}@Overridepublic String toString() {return "Emp{" +"dept=" + dept +'}';}public void test() {System.out.println(dept);}
}
根據(jù)屬性名稱自動注入
<!--實(shí)現(xiàn)自動裝配bean標(biāo)簽屬性autowire,配置自動裝配autowire屬性常用兩個(gè)值:byName根據(jù)屬性名稱注入,注入的值bean的id的值要和類里面屬性的名稱一樣byType根據(jù)屬性的類型注入
-->
<bean id="emp" class="com.zan.spring5.autowire.Emp" autowire="byName">
<!--        <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.zan.spring5.autowire.Dept"></bean>

img

根據(jù)屬性類型自動注入
<!--實(shí)現(xiàn)自動裝配bean標(biāo)簽屬性autowire,配置自動裝配autowire屬性常用兩個(gè)值:byName根據(jù)屬性名稱注入,注入的值bean的id的值要和類里面屬性的名稱一樣byType根據(jù)屬性的類型注入
-->
<bean id="emp" class="com.zan.spring5.autowire.Emp" autowire="byType">
<!--        <property name="dept" ref="dept"></property>-->
</bean>
<bean id="dept" class="com.zan.spring5.autowire.Dept"></bean>

img

  • 注意:用類型會不太好,因?yàn)槿绻袃蓚€(gè)類都是Dept類,那么它將會不知道要使用哪個(gè)Dpet

IOC操作Bean管理(外部屬性文件)

  1. 直接配置數(shù)據(jù)庫信息

    1. 配置德魯伊連接池
    2. 引入德魯伊連接池依賴jar包
<!--直接去配置連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/atguigu"></property><property name="username" value="root"></property><property name="password" value="123456"></property>
</bean>

img

  1. 引入外部屬性文件配置數(shù)據(jù)庫連接池

    1. 創(chuàng)建外部屬性文件,properties格式文件,寫數(shù)據(jù)庫信息
//properties的左邊隨便寫,只是別名
prop.driverClass=com.mysql.cj.jdbc.Driver
prop.url=jdbc:mysql://localhost:3306/atguigu
prop.username=root
prop.password=123456
    1. 把外部properties屬性文件引入到Spring配置文件中
      1. 引入context名稱空間
<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/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
      1. 在Spring配置文件使用標(biāo)簽引入外部屬性文件
<!--直接去配置連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"></property><property name="url" value="jdbc:mysql://localhost:3306/bjpowernode"></property><property name="username" value="root"></property><property name="password" value="root"></property>--><property name="driverClassName" value="${prop.driverClass}"></property><property name="url" value="${prop.url}"></property><property name="username" value="${prop.username}"></property><property name="password" value="${prop.password}"></property>
</bean><!--引入外部屬性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>

img

💖IOC操作Bean管理(基于注解方式)

注解的基本介紹
  1. 注解是代碼特殊標(biāo)記,格式:@注解名稱(屬性名稱=屬性值,屬性名稱=屬性值)
  2. 使用注解,注解作用在類上面,方法上面,屬性上面
  3. 使用注解目的:簡化xml配置
  • Spring針對Bean管理中創(chuàng)建對象提供注解
@Component表示Spring容器中普通的組件
@Service一般用于業(yè)務(wù)邏輯層或者service層
@Controller一般用于外部層
@Repository一般用于dao層或者持久層
  • 特點(diǎn):上面四個(gè)注解功能是一樣的,都可以用來創(chuàng)建bean實(shí)例,沒有什么區(qū)別,只不過為了讓開發(fā)更加規(guī)范
基于注解方式實(shí)現(xiàn)對象創(chuàng)建
  1. 引入依賴 - 在你的Spring文件中的lib文件中的spring-aop

img

  1. 開啟組件掃描

    1. 前提:引入名稱空間context
<!--開啟組件掃描1. 如果掃描多個(gè)包,多個(gè)包使用逗號隔開2. 掃描那個(gè)包的上層目錄
-->
<!-- <context:component-scan base-package="com.zan.spring5.dao, com.zan.spring5.service"></context:component-scan> -->
<context:component-scan base-package="com.zan"></context:component-scan>

img

  1. 創(chuàng)建類,在類上面添加創(chuàng)建對象注解
//在注解里面的value屬性值,可以省略不寫
//不寫默認(rèn)值是類名稱,把首字母小寫,
//即我們這里是UserService,那默認(rèn)就是userService
@Component(value = "userService")//里面是名字,跟<bean id="" class="">類似, id就是value
public class UserService {public void add() {System.out.println("service add.........");}
}

img

img

  • 因此,通過注解,我們在xml中只需要開啟組件掃描就可以了
開啟組件掃描細(xì)節(jié)配置
<!--示例1use-default-filters="false":表示現(xiàn)在不使用默認(rèn)的filter,使用自己配置的filtercontext:include-filter:表示設(shè)置要掃描哪些內(nèi)容示例1解釋:到com.zan中去掃描只帶Controller的類
-->
<context:component-scan base-package="com.zan" use-default-filters="false"><context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan><!--示例2下面的配置是掃描包里的所有的內(nèi)容context:exclude-filter:設(shè)置哪些內(nèi)容不去掃描解釋:到com.zan中掃描,但不掃描帶Controller的類
-->
<context:component-scan base-package="com.zan"><context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

img

基于注解方式實(shí)現(xiàn)屬性注入
  1. @Autowired:根據(jù)屬性類型進(jìn)行自動裝配

    1. 創(chuàng)建service和dao對象,在service和dao類上添加對象注解
    2. 在service注入dao對象,在service類添加dao類型屬性,在屬性上面使用注解
@Component(value = "userDaoImpl1")
public class UserDaoImpl implements UserDao{@Overridepublic void add() {System.out.println("dao add............");}
}//在注解里面的value屬性值,可以省略不寫
//不寫默認(rèn)值是類名稱,把首字母小寫,
//即我們這里是UserService,那默認(rèn)就是userService
@Component(value = "userService")//里面是名字,跟<bean id="" class="">類似, id就是value
public class UserService {//定義dao類型屬性//不需要添加set方法,因?yàn)樵谶@里面已經(jīng)封裝了//只需要添加注入屬性的注解就可以了@Autowired //根據(jù)類型進(jìn)行自動注入private UserDao userDao;public void add() {System.out.println("service add.........");userDao.add();}
}

img

  1. @Qualifier:根據(jù)名稱進(jìn)行注入
  • 注意:這個(gè)@Qualifier注解的使用,需要和@Autowired一起使用
package com.zan.spring5.service;import com.zan.spring5.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;import javax.annotation.Resource;//在注解里面的value屬性值,可以省略不寫
//不寫默認(rèn)值是類名稱,把首字母小寫,
//即我們這里是UserService,那默認(rèn)就是userService
@Component(value = "userService")//里面是名字,跟<bean id="" class="">類似, id就是value
public class UserService {//定義dao類型屬性//不需要添加set方法,因?yàn)樵谶@里面已經(jīng)封裝了//只需要添加注入屬性的注解就可以了@Autowired //根據(jù)類型進(jìn)行自動注入@Qualifier(value = "userDaoImpl1") //根據(jù)名稱注入private UserDao userDao;public void add() {System.out.println("service add.........");userDao.add();}
}
  • 注意:dao類和service類的value必須一致

img

img

  1. @Resource:可以根據(jù)類型注入,也可以根據(jù)名稱注入
//    @Resource //根據(jù)類型進(jìn)行注入
@Resource(name = "userDaoImpl1") //根據(jù)名稱進(jìn)行注入
private UserDao userDao2;
  • 注意:注入Resource這個(gè)類是javax的擴(kuò)展包中的annotation,因此Spring官方建議我們用Autowired和Qualifier
  1. @Value:注入普通類型屬性
@Value(value = "abc")
private String name;public void add() {System.out.println("service add.........");
//        userDao.add();userDao2.add();System.out.println(name);
}

img

完全注解開發(fā)(純注解開發(fā)) - 不需要xml配置文件
  1. 創(chuàng)建配置類,用來替代xml配置文件
  • 這個(gè)配置類,里面不寫東西,相當(dāng)于xml配置文件
package com.zan.spring5.config;import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;@Configuration //表示這個(gè)類作為配置類,來替代xml配置文件
@ComponentScan(basePackages = {"com.zan"}) //表示xml中的開啟組件掃描
public class SpringConfig {}
  1. 測試
@Test
public void testService2() {//加載配置類ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);UserService userService = context.getBean("userService", UserService.class);userService.add();
}

img

http://www.risenshineclean.com/news/57876.html

相關(guān)文章:

  • 上海建設(shè)網(wǎng)站公重慶關(guān)鍵詞搜索排名
  • 廣東網(wǎng)站設(shè)計(jì)公司價(jià)格seo關(guān)鍵詞優(yōu)化系統(tǒng)
  • 免備案空間主機(jī)寧波seo自然優(yōu)化技術(shù)
  • 代賬行業(yè)門戶網(wǎng)站開發(fā)廣告推廣平臺賺取傭金
  • 北京市住房城鄉(xiāng)建設(shè)委官方網(wǎng)站南京seo新浪
  • 怎么做網(wǎng)站安全運(yùn)維天堂網(wǎng)長尾關(guān)鍵詞挖掘網(wǎng)站
  • 外國人做外貿(mào)都會瀏覽哪些網(wǎng)站一鍵注冊所有網(wǎng)站
  • 做企業(yè)平臺的網(wǎng)站百度競價(jià)點(diǎn)擊軟件
  • 網(wǎng)站域名代辦網(wǎng)頁設(shè)計(jì)制作網(wǎng)站圖片
  • wordpress企業(yè)模板免費(fèi)下載常州seo招聘
  • web模板免費(fèi)下載網(wǎng)站上海網(wǎng)站建設(shè)開發(fā)
  • 網(wǎng)站使用微信支付零基礎(chǔ)seo入門教學(xué)
  • 網(wǎng)站的建設(shè)域名空間軟文范例200字
  • 網(wǎng)站制作預(yù)算電商運(yùn)營入門基礎(chǔ)知識
  • 濮陽免費(fèi)網(wǎng)站制作2024年新冠疫情最新消息今天
  • 網(wǎng)站建設(shè)后續(xù)需要維護(hù)怎么用網(wǎng)絡(luò)推廣
  • 佛山市網(wǎng)站建設(shè)分站多少錢seo營銷專員
  • 做個(gè)購物網(wǎng)站多少錢買賣友情鏈接
  • 響應(yīng)式網(wǎng)站什么意思怎樣做好銷售和客戶交流
  • 宛城區(qū)網(wǎng)站推廣seo有哪些優(yōu)缺點(diǎn)?
  • 廣州網(wǎng)站建設(shè)公司哪家好有哪些可以免費(fèi)推廣的平臺
  • 網(wǎng)站的前端怎么做商丘seo
  • 購物網(wǎng)站建設(shè)成本最新新聞消息
  • 前端做網(wǎng)站難嗎哈爾濱最新今日頭條新聞
  • 企業(yè)營銷網(wǎng)站服務(wù)器1g夠個(gè)人怎么開跨境電商店鋪
  • 濟(jì)南網(wǎng)站制作創(chuàng)意百度信息流推廣
  • 專業(yè)做俄語網(wǎng)站建設(shè)司拉人頭最暴利的app
  • 開平做網(wǎng)站重慶關(guān)鍵詞seo排名
  • 網(wǎng)站建設(shè)教程下載上海百度搜索排名優(yōu)化
  • 做網(wǎng)站導(dǎo)流點(diǎn)金推廣優(yōu)化公司