spring-kuang
生活随笔
收集整理的這篇文章主要介紹了
spring-kuang
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
spring創(chuàng)建對(duì)象
思考:spring的控制翻轉(zhuǎn)其實(shí)就是將創(chuàng)建對(duì)象也就是實(shí)例化對(duì)象交給spring去做,當(dāng)你在beans.xml中注冊(cè)了一個(gè)bean時(shí),spring已經(jīng)幫你實(shí)例化好了,你只需要在用的時(shí)候從ApplicationContext中取出來即可。而我們?cè)诓挥胹pring的時(shí)候都是什么時(shí)候使用對(duì)象的時(shí)候再去創(chuàng)建對(duì)象然后才能使用你所創(chuàng)建的對(duì)象,這樣就是控制反轉(zhuǎn),將控制權(quán)交給spring容器。
取別名只是多了個(gè)名字
不同類型參數(shù)注入
各類型屬性參數(shù)值注入練習(xí)
c命名空間和p命名空間
ByType可以省略id名,因?yàn)锽yType是根據(jù)對(duì)象屬性類型來自動(dòng)裝配的
使用了@Autowired注解可以省略set方法
注意點(diǎn)
這種情況personxml配置中aotuwired=“byname” 和 不填 都可以自動(dòng)裝配id為"dog119"的對(duì)象和id為"cat22"的對(duì)象
但是如果autowire=“byType” 就會(huì)報(bào)錯(cuò),原因是xml文件中配置了多個(gè)對(duì)象,類型都相同,但是名字不同,xml按照類型去自動(dòng)裝配的話不知道應(yīng)該裝配哪個(gè)對(duì)象,就會(huì)報(bào)錯(cuò)
========================================================================================
使用注解開發(fā)
@Component
在實(shí)體類上加上@Component注解后就不用在beans.xml中注冊(cè)這個(gè)bean了,但是沒有id,在context.getBean(“XX”)方法中所填的id默認(rèn)為類名小寫
@value
衍生注解
@Configuration和@bean
@Configuration標(biāo)注在類上,相當(dāng)于把該類作為spring的xml配置文件中的<beans>,作用為:配置spring容器(應(yīng)用上下文)package com.dsx.demo;import org.springframework.context.annotation.Configuration;@Configuration public class TestConfiguration {public TestConfiguration() {......} }這個(gè)注解加在哪個(gè)類上,就相當(dāng)于這個(gè)類變成了ApplicationContext.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" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:util="http://www.springframework.org/schema/util" xmlns:task="http://www.springframework.org/schema/task" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsdhttp://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsdhttp://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-4.0.xsd" default-lazy-init="false"></beans>再將@bean注解加在具體的一個(gè)類上,就相當(dāng)于在這個(gè)xml文件中注冊(cè)一個(gè)bean,而且這個(gè)類一定是返回pojo包下的某個(gè)實(shí)體類對(duì)象,比如
注意點(diǎn):(1)@Bean注解在返回實(shí)例的方法上,如果未通過@Bean指定bean的名稱,則默認(rèn)與標(biāo)注的方法名相同;
舉例:取得這個(gè)bean對(duì)象時(shí),我們的id值使用的是getuser----->context.getBean(“getUser”)
(2)@Bean注解默認(rèn)作用域?yàn)閱卫齭ingleton作用域,可通過@Scope(“prototype”)設(shè)置為原型作用域
(3)既然@Bean的作用是注冊(cè)bean對(duì)象,那么完全可以使用@Component、@Controller、@Service、@Repository等注解注冊(cè)bean(在需要注冊(cè)的類上加注解),當(dāng)然需要配置@ComponentScan注解進(jìn)行自動(dòng)掃描。
實(shí)體類
配置類
package com.kuang.config;import com.kuang.pojo.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration;@Configuration @ComponentScan("com.kuang.pojo") public class Config {@Beanpublic User getUser(){return new User();} }測試代碼
import com.kuang.config.Config; import com.kuang.pojo.User; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;public class Test {@org.junit.Testpublic void test(){ApplicationContext context = new AnnotationConfigApplicationContext(Config.class);User user = (User) context.getBean("getUser");System.out.println(user);}}測試代碼目錄結(jié)構(gòu)
動(dòng)態(tài)代理AND AOP
方式一:使用spring的接口實(shí)現(xiàn)(MethodBeforeAdvice、AfterReturingAdvice兩個(gè)接口)
主要是springAPI接口實(shí)現(xiàn)
方式二:使用自定義的類和方法作為切面和通知
主要是切面定義
注解實(shí)現(xiàn)aop
aop:在不影響原業(yè)務(wù)代碼的基礎(chǔ)上增強(qiáng)業(yè)務(wù)
注解aop測試代碼
使用aop需要一個(gè)aop織入依賴,如下
aspectjweaver是spring的切入點(diǎn)表達(dá)式需要用的包 <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.8.4</version></dependency>整合mybatis需要用到的依賴:
spring整合mybatis
spring配置數(shù)據(jù)源需要兩個(gè)東西:一個(gè)是spring-jdbc的依賴,另一個(gè)就是該包下的DriverManagerDataSource類
第一步:配置數(shù)據(jù)源
遇到的問題:在spring中配置mybatis時(shí)由于全類名太長只能記住DriverManagerDataSource,而spring沒有提示全類名,就需要去file—setting—plugin中搜索spring,將spring相關(guān)插件勾選上,再重啟IDEA就可以看到提示了
第二部:配置sqlsessionFactory
在spring-mybatis中,創(chuàng)建sqlSessionFactory是使用sqlSessionFactoryBean這個(gè)對(duì)象,并在property中配置數(shù)據(jù)源,這樣sqlsessionfatory便在spring中創(chuàng)建好了
sqlsessionfatory的bean注冊(cè)中還可以做其他在mybatis配置文件中所做的事情,注冊(cè)mapper;還可以綁定mybatis-config.xml文件,這樣的話,spring和mybatis兩個(gè)配置文件就聯(lián)系起來了
第三步:注冊(cè)sqlsession對(duì)象
在spring中sqlsession叫做sqlsessiontemplate,注冊(cè)SqlSessionTemplate時(shí)需要將SqlSessionFactory作為SqlSessionTemplate的構(gòu)造器參數(shù)注入,因?yàn)樵趧?chuàng)建SqlSessionTemplate對(duì)象時(shí)需要用到構(gòu)造器,而SqlSessionTemplate沒有無參構(gòu)造器,也沒有set方法,只能通過有參構(gòu)造器的方式創(chuàng)建,而傳入這個(gè)有參構(gòu)造器的參數(shù)便是SqlSessionFactory,我們只需引入SqlSessionFactory即可
第四步:編寫UserMapper實(shí)現(xiàn)類
要記得將這個(gè)實(shí)現(xiàn)類注冊(cè)到spring中!并傳入?yún)?shù):sqlsessiontemplate
spring-mybatis整合demo代碼
- 項(xiàng)目結(jié)構(gòu):
- mybatis配置文件:
- spring配置文件:
- UserMapper.class
- UserMapperImpl.class
- UserMapper.xml文件
在spring中獲取sqlsession的第二種方式:
讓UserMapperImpl類在實(shí)現(xiàn)UserMapper接口的同時(shí)繼承SqlSessionDaoSupport類,類中有g(shù)etSqlsession方法可以直接獲取sqlsession
當(dāng)然,繼承了SqlSessionDaoSupport類的實(shí)現(xiàn)類也需要在spring中注冊(cè)一個(gè)bean
spring事務(wù)
使用事務(wù)一定需要導(dǎo)入aspectjweaver包
總結(jié)
以上是生活随笔為你收集整理的spring-kuang的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ken的第一篇
- 下一篇: 计算机组成与嵌入式系统 百度云,计算机组