스프링 프레임 워크는 소프트웨어 개발의 복잡성으로 인해 생성됩니다. Spring은 기본 Javabeans를 사용하여 이전에 EJB가 가능했던 일을합니다. 그러나 Spring의 목적은 서버 측 개발에만 국한되지 않습니다. 단순성, 테스트 가능성 및 느슨한 커플 링의 관점에서 볼 때 대부분의 Java 응용 프로그램은 스프링의 혜택을 누릴 수 있습니다. Spring은 가벼운 제어 역전 (IOC) 및 섹션 지향 (AOP) 컨테이너 프레임 워크입니다.
◆ 목적 : 엔터프라이즈 애플리케이션 개발의 복잡성을 해결하십시오
◆ 기능 : EJB 대신 기본 Javabean을 사용하고 더 많은 엔터프라이즈 애플리케이션 기능을 제공합니다.
◆ 범위 : 모든 Java 응용 프로그램
제어의 역전 (영어 약어의 IOC)은 프레임 워크의 중요한 기능이며 객체 지향 프로그래밍을위한 특별한 용어가 아닙니다. 의존성 주입 및 종속성 조회가 포함됩니다. 기존 비즈니스 계층에서 자원이 필요한 경우 비즈니스 계층에서 새로운 리소스가 발견되어 커플 링 (프로그램 간의 상호 의존성 및 상관 관계)이 더 높습니다. 이제 새로운 부품을 스프링으로 넘겨서 높은 응집력과 낮은 커플 링을 달성하십시오. 요컨대 : 원래 DAO 계층 또는 서비스 계층 방법이 호출 될 때마다 앱은 새로운 것을 사용하고 이제는 새로운 권리가 스프링으로 넘겨졌으며 봄부터 필요한 자원이 얻어졌습니다!
1. 프레임 워크에 필요한 종속성 JAR 패키지 다운로드
공식 봄 웹 사이트는 다음과 같습니다. http://spring.io/
JAR 패키지를 다운로드하십시오 : http://repo.springsource.org/libs-release-local/org/springframework/spring
2. 기본 JAR 패키지를 가져옵니다
실제로, 기본적인 핵심 항아리에는 콩; 컨텍스트; 핵심; 발현 패키지가 포함되고, 다른 핵심 패키지는 포함되며, 다른 핵심은 Log4J 로그에 의존합니다. 물론, 봄 항아리는 그 이상이며, 이후 단계에서 천천히 추가됩니다.
3. log4j 구성 파일을 구성합니다
로그 파일은 SRC 디렉토리에 정의되어 있습니다
### stdout에 직접 로그 메시지 ### log4j.appender.stdout = org.apache.log4j.consoleAppenderlog4j.appender.stdout.target = system.errlog4j.appender.stdout.layout = org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversonversonpattern = %d { 비 ### log4j.appender.file = org.apache.log4j.fileAppenderLog4j.appender.file.file = c/: mylog.loglog4j.appender.file.layout = org.apache.log4j.patternlayoutlog4j.appender.file.layout.conversonpattern = %d {retute 비4. 로그 파일이 성공적으로 배포되었는지 테스트하십시오
패키지 com.clj.demo1; import org.apache.log4j.logger; import org.junit.test;/** * 데모 로그 사용법 * @Author Administrator */public class demo1 {// log 클래스 비공개 logger log = logger.getLogger (demo1.class); @test public void run1 () {// log4j.rootlogger 속성에서 정보를 OFF로 변경하고 log.info ( "execute"); }}5. 인터페이스를 정의하고 클래스를 구현하십시오
인터페이스 :
package com.clj.demo2; public interface userservice {public void sayhello ();}구현 클래스
package com.clj.demo2; public class userserviceimpl은 userervice {private string name; 공개 문자열 getName () {return name; } public void setName (문자열 이름) {this.name = 이름; } public void init () {system.out.println ( "초기화 .."); } public void sayhello () {system.out.println ( "hello spring"+"/t"+name); } public void vocentory () {System.out.println ( "Destroy .."); }}6. 스프링 별 구성 파일을 정의하십시오
ApplicationContext.xml의 이름을 정의하십시오. 위치는 SRC이며 로그 파일과 동일한 디렉토리, 해당 제약 조건을 가져 오며 구현 클래스를 구성 파일에 주입합니다. 처음부터 시작하고 Bean 제약 조건을 사용하십시오
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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/sprone--bean.sd" 1. ID 값은 고유합니다 (필기 필기) 2. 참고 : 클래스는 인터페이스가 아닌 구현 클래스 경로입니다 (필기 꼭 쓰기) 3. 핵심 메소드가 실행되기 전 초기화 작업 (쓰기 선택) 4. 핵심 메소드가 실행 된 후 초기화 작업 (<bean id = eNIC”init-metHod = "init"ait avent ""destory "> <names ="jaxiansen "> </property" </bean> </beans>
7. 테스트
공개 클래스 demo1 { /*** 원본 방법* /@test public void run () {// 구현 클래스 생성 userserviceimpl s = new UserserviceImpl (); s.setname ( "jaxiansen"); s.sayhello (); } / *** Old Factory 버전 BeanFactory* Old Factory는 구성 파일 객체를 생성하지 않습니다* / @test public void run2 () {beanfactory factory = new xmlBeanFactory (new ClassPathResource ( "ApplicationContext.xml")); UserserVice US = (userService) factory.getBean ( "userService"); us.sayhello (); } /*** Spring Framework 사용 iOC 메소드* 새 버전의 공장에서 시작 서버를 만들기 위해 구성 파일 개체를 만듭니다.* /@test public void run3 () {// 콜로리 구성 파일을로드하고 코어 구성 파일을로드 할 때 공장을로드 할 필요가 없습니다. classPathXmlApplicationContext ( "ApplicationContext.xml"); // 공장에서 객체를 가져옵니다 (구성 파일의 ID 값, 여기서 다형성이 사용됩니다) userservice usi = (userservice) ac.getBean ( "userService"); // usi.sayhello ()를 실행하기 위해 객체의 메소드를 호출합니다. } /*** Demo Destrove-eThod 메서드* Bean Destroy 방법은 자동으로 실행되지 않습니다* SCOPE = SINGLETON 또는 웹 컨테이너에서 자동으로 호출되지 않는 한, 주요 기능 또는 테스트 케이스는 수동으로 호출되어야합니다 (ClassPathXMlapplicationContext의 Close () 메소드를 사용해야합니다)* /@test public void run4 () {// a Core Public void run4 () {// Core Public void run4 () { /@test public void run4 () { /@test public void run4 () (classPathXmlApplicationContext는 src에서 발견됩니다) classPathXmlApplicationContext ac = new ClassPathXmlApplicationContext ( "ApplicationContext.xml"); // 공장에서 객체를 가져옵니다 (구성 파일의 ID 값, 여기서 다형성이 사용됩니다) userservice usi = (userservice) ac.getBean ( "userService"); // usi.sayhello ()를 실행하기 위해 객체의 메소드를 호출합니다. // ApplicationContext 구현 클래스는 긴밀한 메소드를 제공하며 공장을 닫을 수 있고 Destory-Method 메소드를 실행할 수 있습니다. }}오래된 공장과 새로운 공장의 차이
* BeanFactory와 ApplicationContext의 차이
* beanfactory- Beanfactory는 게으른 하중을 가져 가고, 처음으로 얻는 경우에만 콩이 초기화됩니다.
* ApplicationContext -ApplicationContext.xml로드시 Bean 객체의 특정 인스턴스가 생성되고 다른 기능이 제공됩니다.
* 이벤트 배달
* 콩 자동 조립
* 다양한 응용 프로그램 계층의 컨텍스트 구현
요약 : 이것은 구현 클래스를 스프링 구성 파일로 구성하는 가장 기본적인 데모입니다. 서버가 시작될 때마다 구성 파일이로드되어 구현 클래스가 인스턴스화됩니다.
1. 의존성 주입이란 무엇입니까?
Spring은 J2EE 응용 프로그램 수준의 객체를 효과적으로 구성 할 수 있습니다. 그것이 제어 계층의 동작 객체, 비즈니스 계층의 서비스 객체 또는 지속성 계층의 DAO 객체이든 스프링 관리하에 유기적으로 조정하고 실행할 수 있습니다. 스프링은 느슨하게 결합 된 방식으로 각 층의 물체를 함께 구성합니다. 액션 객체는 서비스 객체의 특정 구현에 대해 신경 쓰지 않아도되며, 서비스 객체는 영구 레이어 개체의 특정 구현에 대해 신경 쓰지 않아도되며 각 레이어 객체에 대한 호출은 완전히 인터페이스 지향적입니다. 시스템을 리팩토링 해야하는 경우 코드 재 작성의 양이 크게 줄어 듭니다. 종속성 주입은 콩과 빈을 하드 코딩되지 않고 구성 파일로 구성합니다. 의존성 주입을 이해하십시오
의존성 주입 및 제어의 역전은 동일한 개념입니다. 구체적인 의미는 다음과 같습니다. 역할 (아마도 Java 인스턴스, 발신자)이 다른 역할 (또 다른 Java 인스턴스, 발신자)의 도움이 필요할 때 전통적인 프로그래밍 프로세스에서 발신자는 일반적으로 발신자가 생성합니다. 그러나 봄에는 칼리를 만드는 작업은 더 이상 발신자가 수행하지 않으므로 대조 역전이라고합니다. Callee 인스턴스를 만드는 작업은 일반적으로 스프링 컨테이너에 의해 수행 된 다음 발신자에 주입되므로 종속성 주입이라고도합니다.
의존성 주입 또는 제어 역전이든, Spring은 다양한 객체를 관리하는 역동적이고 유연한 방법을 채택 함을 의미합니다. 객체 간의 특정 구현은 서로 투명합니다.
2. IOC와 Di의 개념
* IOC- 제어의 역전, 제어 역전, 물체의 생성을 스프링으로 바꾸십시오! !
* DI- 종속성 주입, 종속성 주입, 스프링 프레임 워크가 Bean 객체를 생성하고 종속성 객체를 Bean 구성 요소에 동적으로 주입하는 경우! !
3. 데모
클래스 멤버 변수의 경우 두 가지 공통 주입 방법이 있습니다.
속성 세트 방법 주입 및 생성자 메소드 주입
먼저 첫 번째 유형을 보여줍니다. 속성 세트 메소드 주입
1) 지속적인 층
package com.clj.demo3; public class customer daoimpl {public void save () {system.out.println ( "나는 지속성 계층의 dao"); }}2) 비즈니스 계층
참고 : 현재, 지속성 계층을 비즈니스 계층에 주입하고 끈기 계층 인스턴스를 프레임 워크에 제작하기 위해 권리를 넘겨주고 싶습니다. 조건은 비즈니스 계층이 멤버 속성을 제공하고 지속성 계층의 메소드를 설정해야합니다.
패키지 com.clj.demo3;/** * 종속성 주입은 서비스 계층에 dao 계층을 주입합니다 * @author 관리자 * */public class customerserviceimpl {// 멤버 조디악 제공 세트 메소드 제공 개인 고객 daoimpl customerdao; Public Void SetCustomerDao (CustomerDaoimpl CustomerDao) {this.CustomerDao = CustomerDao; } public void save () {system.out.println ( "나는 서비스 ..."); // 1. 원본 방법 // new CustomerDaoimpl (). save (); //2.spring ioc method customerdao.save (); }}3) 구성 파일 구성
<!-시연 종속성 주입-> <bean id = "customerdao"/> <bean id = "customerservice"> <!-서비스 계층에 dao를 주입합니다-> <property name = "customerdao"ref = "customerdao"> </property> </bean>
4) 테스트
/** * 스프링 종속성 주입 방법 * 서비스 계층에 dao 계층을 주입 */@test public void run2 () {// 팩토리 생성, 구성 파일을로드하고 customerservice가 생성되어 CustomerDao ApplicationContext context = new ClassPathXmlApplicationContxt ( "ApplicationContext.xml"); customerserviceimpl csi = (customerserviceimpl) context.getBean ( "customerVice"); csi.save (); }두 번째 유형 : 시공 방법 주입
1) POJO 클래스 및 생성자 방법을 제공합니다
패키지 com.clj.demo4;/** * 데모 주입 방법 * @author 관리자 * */public class car1 {private String cname; 개인 이중 가격; public car1 (String cname, double price) {super (); this.cname = cname; this.price = 가격; } @override public String toString () {return "car1 [cname =" + cname + ", price =" + price + "]; }}2) 구성 파일 구성
<!-건축 방법 주입 방법-> <bean id = "car1"> <!-쓰기 메소드 1 <constructor-arg name = "cname"value = "bmw"/> <constructor-arg name = "price"value = "400000"/>-> <! <constructor-arg index = "0"0 "/>>>>>>. 값 = "400000"/> </bean>
3) 테스트
@Test public void run1 () {ApplicationContext ac = new ClassPathXmlApplicationContext ( "ApplicationContext.xml"); car1 car = (car1) ac.getbean ( "car1"); System.out.println (자동차); }확장 : 구성 메소드 구성은 한 객체를 다른 객체에 주입합니다
1) POJO 클래스 : 목적 : 위의 열에서 차를 인간에게 주입하여 속성 중 하나로 만듭니다. 이 클래스에서는 자동차의 회원 속성을 제공해야하며 매개 변수화 된 시공 방법이 제공되어야합니다.
package com.clj.demo4; public class person {개인 문자열 이름; 개인 CAR1 CAR1; 공개 사람 (문자열 이름, car1 car1) {super (); this.name = 이름; this.car1 = car1; } @Override public String toString () {return "person [name =" + name + ", car1 =" + car1 + "]; }}2) 구성 파일
<!-생성자 -ARG 이름 = "name"value = "jaxiansen"/> <생성자-arg name = "car1"ref = "car1"/> </bean>
4. 컬렉션 배열을 주입하는 방법
1) pojo 클래스를 정의하십시오
package com.clj.demo4; import java.util.arrays; import java.util.list; import java.util.map; import java.util.properties; import java.util.set;/** * 주입을 설정하는 방법을 입증하십시오 * @author user {] ARTRRS [] ARTRRS; 개인 목록 <String> 목록; 개인 세트 <문자열> 세트; 개인지도 <문자열, 문자열>지도; 개인 부동산 프로; public void setpro (Properties Pro) {this.pro = pro; } public void setsets (set <string> sets) {this.sets = sets; } public void setMap (map <string, String> map) {this.map = map; } public void setList (list <string> list) {this.list = list; } public void setArrrs (String [] arrrs) {this.arrs = arrrs; } @override public String toString () {return "user [arrs =" + arrays.tostring (arrs) + ", list =" + list + ", sets =" + sets + ", map =" + map + ", pro =" + pro + "]; }}2) 구성 파일
<!-주입 세트-> <bean id = "user"> <!-array-> <속성 이름 = "arrs"> <list> <value> value> <value> 숫자 2 </value> <value> number3 </value> </list> <! <!-list set-> <property name = "list"> <list> <value> <value> <value> <value> </property> <!-세트 세트-> <property name = "sets"> <value> haha </value> <value> haha </value> </value> </set> </set> </property> </set> </property> </value> </value> </value> </value> </value> </value> </<propert key = "map"> <map> <Entry Key = "value ="rainbow "/> <enlick key ="value "value"<! 이름 = "pro"> <props> <prop key = "username"> root </prop> <prop key = "password"> 123 </prop> </props> </propert> </bean>
3) 테스트
/ *** 테스트 주입 컬렉션*/ @test public void run3 () {ApplicationContext ac = new ClassPathXmlApplicationContext ( "ApplicationContext.xml"); 사용자 user = (사용자) ac.getBean ( "사용자"); System.out.println (사용자); }5. 모듈에서 개발하는 방법
기본 구성 파일에 <import> 태그를 추가합니다 (구성 파일 ApplicationContext2.xml이 com.clj.test 패키지에 정의되었다고 가정합니다).
<!-모듈 개발에 의해 다른 구성 파일 소개-> <가져 오기 자원 = "com/clj/test/applicationcontext2.xml"/>
1. 시작하기
1)
이전 6 개의 패키지 외에도 주석을 달성하려면 스프링 aop 패키지가 필요합니다.
2). 지속성 계층 및 구현 레이어 (인터페이스는 여기에서 무시됩니다)
지속적인 층
package com.clj.demo1; import org.springframework.context.annotation.scope; import org.springframework.stereotyp.component; import org.springframework.stereotype.repository;/** * userdaoimpl에 의해 Ioc에 의해 손을 뻗은 컨테이너 관리자 */public userdao us userdao ordaoimpl pless ordaoimpl ancounder us oursultor user us userda us us userdations us userda us us us us us us userdation @override public void save () {system.out.println ( "클라이언트 저장 .."); }}비즈니스 계층
패키지 com.clj.demo1; import javax.annotation.postconstruct; import org.springframework.beans.ackory.annotation.autowired; import org.springframework.bean.beans.annotation.qualifier; import org.springframework.beans.annot.value; import; org.springframework.stereotype.component; public class usererviceimpl은 userervice {@override public void sayhello () {system.out.println ( "hello spring"); }}3). 구성 파일을 정의하십시오
현재 제약 조건은 컨텍스트 제약 조건을 추가하고 구성 요소 스캔을 추가해야합니다.
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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/schema/sprideans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-Bean 정의 여기-> <!-기본 ---package 스캔 패키지-<context : concent : component : scancifies scan scan scan scan-< Base-Package = "com.clj.demo1"/> </beans>
4) 구현 클래스에 주석을 추가하십시오
/*** 현재 클래스를 표시하는 데 사용할 수있는 구성 요소 주석* <bean id = "userservice"와 유사한 값* value*/@component (value = "userErvice") 공개 클래스 userserviceimpl emerservice {// axitted}에 대한 별명을 제공하는 것을 의미합니다.5) 쓰기 테스트
/ *** 주석 메소드*/ @test public void run2 () {ApplicationContext ac = new ClassPathXmlApplicationContext ( "ApplicationContext.xml"); userervice us = (userservice) ac.getbean ( "uservice"); us.sayhello (); }2. Bean Management Common 속성에 대해
1. @Component : 구성 요소. (수업에 따라 행동) 가장 원시적 인 주석, 주석이 필요한 모든 클래스에 대해 이것을 작성해도 괜찮습니다. 일반적으로 일반적입니다.
2. @Component의 세 가지 미분 주석이 봄에 제공됩니다. (기능은 현재 일관성이 있습니다)
* @Controller- 웹 계층에서 작동합니다
* @Service- 비즈니스 수준에서 활동합니다
* @repository- 지속성 계층에서 작용합니다
* 참고 :이 세 가지 주석은 주석 클래스 자체의 목적을 명확하게하기위한 것이며 Spring은 후속 버전으로이를 향상시킵니다.
3. 속성 주입에 대한 주석 (참고 : 주석 주입을 사용할 때는 세트 방법을 제공 할 필요가 없습니다).
* 일반적인 주입 유형 인 경우 값 주석을 사용할 수 있습니다.
* @Value- 일반 유형을 주입합니다
* 주입 된 객체 유형 인 경우 다음 주석을 사용하십시오.
* @autowired- 기본적으로 유형은 자동으로 유형별로 조립되며 주입 된 클래스의 클래스 이름과 관련이 없습니다.
* 이름으로 주사하고 싶다면
* @Qualifier- 이름 주입을 강제로 사용하여 Autowired와 함께 사용하고 클래스 이름을 지정하며 주입 된 클래스 이름과 관련이 있어야합니다.
* @Resource- @autowired 및 @qualifier에 해당합니다
* 강조 : Java가 제공하는 주석
* 속성은 이름 속성을 사용합니다
4. 콩 범위의 주석
* 클래스에 사용되는 @Scope (value = "Prototype")로 주석을 달았습니다. 값은 다음과 같습니다.
* 싱글 톤 - 싱글 톤, 기본값
* 프로토 타입 - 여러 사례
5. 콩의 수명주기 구성 (이해)
* 주석은 다음과 같습니다.
* @PostConstruct- 초기 방법에 해당합니다
* @predestroy- 파괴 방법에 해당합니다
1. 속성 객체 주석을 보여줍니다
조건 : 스캔하여 속성 (이름) 및 객체 (userDaoImpl)를 비즈니스 계층에 주입합니다.
1) 지속성 층을 열어 주석을 스캔하십시오
// component (value = "userDao") Universal Class Annotation@repository (value = "ud") public class userdaoimpl userdao {@override public void save () {system.out.println ( "클라이언트 저장"); }}2) 비즈니스 계층은 속성 및 개체에 대한 주석을 제공합니다.
패키지 com.clj.demo1; import javax.annotation.postconstruct; import org.springframework.beans.ackory.annotation.autowired; import org.springframework.bean.beans.annotation.qualifier; import org.springframework.beans.annot.value; import; org.springframework.stereotyp.component;/** * 구성 요소 주석은 <bean id = "uservice"> * value와 유사한 현재 클래스 *를 표시하는 데 사용될 수 있습니다. 주석 : 지정된 문자열을 이름 속성에 주입하는 것과 같습니다. setName 메소드는 @Value (value = "jaxiansen") 개인 문자열 이름을 쓰지 않고 생략 할 수 있습니다. /** * 참조 주입 방법 1 : autowired () * 참조 주입 방법 2 : autowired () + Qualifier * 참조 주입 메소드 3 : @Resource (name = "userDao") Java 메소드, 이름별로 주입을 식별 *//autowired () 유형에 의해 자동 조립되고 주입됩니다 (단체에 의해 일치하지 않기 때문에) @afoundate (aforeate) @autured. @Qualifier (value = "ud") // 이름별로 주입해야합니다. Autowired와 함께 사용해야합니다. 둘 다 클래스 개인 userdao userdao를 지정할 수 있습니다. // Qualifier의 값은 userdaoimpl 클래스 이름의 상단에있는 주석 이름이거나 구성 파일에서 Bean의 ID 이름을 지정할 수 있습니다./*public void setName (String name) {this.name = name; }*/ @override public void sayhello () {system.out.println ( "hello spring"+name); userdao.save (); } // action의 초기화를위한 @postconstruct 태그 주석은 apportconstruct public void init () {system.out.println ( "초기화 ..."); }}3) 모든 구성 파일을 스캔하려면 구성 파일 만 활성화하면됩니다.
<? xml 버전 = "1.0"alcoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://ww.w.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/schema/sprideans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-Bean 정의 여기-> <!-기본 ---package 스캔 패키지-<context : concent : component : scancifies scan scan scan scan-< Base-Package = "com.clj.demo1"/> </beans>
참고 : 컬렉션의 경우 구성 파일을 사용하는 것이 좋습니다.
2. Spring Framework는 Junit 장치 테스트를 통합합니다
1) 필요한 종속성 패키지 스프링 테스트를 추가하십시오
참고 : MyEclipes에는 자체 주니 환경이 제공되지만 때로는 버전 문제로 인해 새로운 주니트 환경이 필요할 수 있습니다. 여기에서 온라인으로 새로운 Junit-4.9 Jar 패키지를 다운로드했습니다. MyEclipes가 더 새로운 경우 고려할 필요가 없습니다.
2) 테스트 클래스를 작성하고 해당 주석을 추가하십시오
@RunWith 및 @ContextConfiguration (Webroot의 기본 경로는 첫 번째 수준 디렉토리이기 때문에 구성 파일을로드하는 데 사용됩니다. 이는 SRC가 첫 번째 레벨 디렉토리인지 결정하는 것입니다).
package com.clj.demo2; import javax.annotation.resource; import org.junit.test; import org.junit.runner.runwith; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junitc.sprringjunit 4.springjunit 4 com.clj.demo1.userservice; @runwith (springjunit4classrunner.class) @contextConfiguration ( "classPath : ApplicationContext.xml") public class demo2 {@resource (names = "userervice") private userservice; @test public void run1 () {userservice.sayhello (); }}6. 스프링 프레임 워크의 AOP
1. AOP는 무엇입니까?
* 소프트웨어 산업에서 AOP는 측면 지향 프로그래밍의 약어입니다.
* AOP
* AOP는 AOP Alliance Organization에서 처음 제안했으며 일련의 규범을 공식화했습니다. Spring은 AOP 아이디어를 프레임 워크에 소개했으며 AOP Alliance 사양을 준수해야합니다.
* 런타임 동안 사전 컴파일 및 동적 에이전트를 통해 프로그램 기능의 통합 유지 보수를 달성하는 기술
* AOP는 OOP, 소프트웨어 개발의 뜨거운 주제, 스프링 프레임 워크의 중요한 부분 및 기능 프로그래밍의 파생 패러다임의 연속입니다.
* AOP를 사용하면 비즈니스 로직의 여러 부분을 분리하여 비즈니스 로직의 일부 사이의 커플 링을 줄이고 프로그램의 재사용 성을 향상시키고 개발 효율성을 향상시킬 수 있습니다.
AOP는 기존의 수직 상속 시스템의 반복 코드를 대체하여 수평 추출 메커니즘을 채택합니다 (성능 모니터링, 트랜잭션 관리, 보안 검사, 캐싱)
2. 왜 AOP를 공부합니다
* 소스 코드를 수정하지 않고 프로그램을 향상시킬 수 있습니다! ! (고정 메소드에 대한 프록시를 만듭니다. 메소드에 액세스하기 전에 프록시를 먼저 입력하십시오. 프록시에서는 더 많은 기능을 작성하여 방법을 더욱 강력하게 만들고 프로그램을 향상시킬 수 있습니다).
AOP : 지향적 프로그래밍, 모든 모듈화, 각 모듈은 비교적 독립적이며 모듈을 공유 할 수 있으며 (동일), 다른 모듈은 특히 사용자 정의됩니다. 프로그램 재사용성을 향상시키기 위해 기존의 수직 프로그래밍 대신 이것을 사용하십시오.
3. AOP 구현 (구현 원리)
AOP의 구현에는 클래스 인터페이스를 구현하기위한 두 가지 프록시 방법 <1>가 포함되어 있습니다.
1. JDK 동적 프록시를 구현하십시오
1) 지속성 계층 인터페이스 구현 클래스를 정의하십시오
package com.clj.demo3; public interface userdao {public void save (); public void update ();} package com.clj.demo3; public class userDaoimpl userDao {@override public void save () {system.out.println ( "저장 사용자"); } @override public void update () {system.out.println ( "modify user"); }}2) JDK 동적 프록시 도구 클래스를 정의하십시오
이 도구 클래스는 지속성 계층 저장 메소드를 실행할 때 일부 기능을 추가하고 개발 중에 소스 코드를 변경하지 않고 메소드를 향상시켜야합니다.
package com.clj.demo3; import java.lang.reflect.invocationHandler; import java.lang.reflect.method; import java.lang.reflect.proxy;/** * jdk (Apposition Aop 원칙) * @Author Admations (Proplic Classe) MyprosyUtils {public classe or getproxyUtils {problic useproxyutils {aplation aop arinciples) * import java.lang.reflect.method; import java.lang.reflect.proxy;/** * userdao dao) {// 프록시 클래스를 사용하여 프록시 객체를 생성하기 위해 userdao proxy = (userdao) proxy.newproxyinstance (dao.getclass (). getClassLoader (), dao.getClass (), getInterfaces (), 새로운 invocationHandler () {//가 실행 되 자마자, getclass () {// invoke (개체 프록시, 메소드 메소드, 오브젝트 [] args) 던지기 가능 {// 프록시는 현재 대상 객체를 나타냅니다. // args acapsed 매개 변수 // 클래스 저장 또는 업데이트 메소드가 정상적으로 실행하도록하십시오 ( "save" method.invoke (dao, args); 리턴 프록시; }}3) 테스트
package com.clj.demo3; import org.junit.test; public class demo1 {@test public void run1 () {// 대상 객체 userdao dao = new userDaoimpl (); dao.save (); dao.update (); System.out.println ( "============================================================================= ======================================================================================================================================== 도구 클래스를 사용하여 프록시를 받으십시오.2. CGLIB 기술 구현
1) 지속성 층을 정의하면 현재 인터페이스가 없습니다.
package com.clj.demo4; public class bookdaoimpl {public void save () {system.out.println ( "저장 책"); } public void ustud () {system.out.println ( "수정 책"); }}2) 도구 수업을 작성하십시오
package com.clj.demo4; import java.lang.reflect.method; import org.springframework.cglib.proxy.enhancer; import org.springframework.cglib.proxy.method interceptor; import org.springframework.cglib.methproxy;/* infore of infiple of Principle of Principle의 importodinterceptor; 프록시 방법 * @Author 관리자 * */public class mycglibitils {/** * cglib 메소드를 사용하여 프록시 객체 생성 * @return */public static bookdaoimpl getProxy () {Enhancer Enhancer = new Enhancer (); // 부모 클래스 Enhancer.SetSuperClass (BookDaoImpl.class)를 설정합니다. // 콜백 함수 Enhancer.setCallback 설정 (new MethodInterceptor () {@Override public 객체 intercept (개체, 메소드 메소드, OBJS, MethodProxy MethodProxy) Throws Throws Throws {If ( "Save")) {System.out.println ( "I Saved"); } return methodProxy.inVokesuper (obj, objs); // 실행 된 메소드}); // 프록시 객체 생성 BookDaoimpl proxy = (BookDaoImpl) enhancer.create (); 리턴 프록시; }}3) 테스트 클래스를 작성하십시오
package com.clj.demo4; import org.junit.test; public class demo1 {@test public void run1 () {// target object bookdaoimpl dao = new bookdaoimpl (); dao.save (); dao.update (); System.out.println ( "====================================)3. Spring 's A Provect J를 기반으로 한 Spring의 AOP 개발 (구성 파일 방법)
1) 환경을 배포하고 해당 JAR 패키지를 가져옵니다.
2) 구성 파일을 생성하고 AOP 제약 조건을 도입하십시오
<beans xmlns = "http://www.springframework.org/schema/beans"xmlns : xsi = "http://www.w3.org/2001/xmlschema-instance"xmlns : aop = "http://wwww.spramfram.org. XSI : schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop http://wwwww.spramframegramegramegramegromegramework.spromegramework.sprome http://www.springframework.org/schema/aop/spring-aop.xsd ">
3) 인터페이스를 만들고 클래스를 구현하십시오
package com.clj.demo5; public interface customerdao {public void save (); public void update ();} 패키지 com.clj.demo5;/** * 구성 파일을 사용하여 aop * @author 관리자 */public class customer daoimpl은 customerdao {@override public void save () {// int a = 10/0; System.out.println ( "고객 저장"); } @override public void update () {// todo 자동 생성 메소드 스터브 시스템.out.println ( "고객 업데이트"); }}4) 패싯 클래스를 정의하십시오
패키지 com.clj.demo5; import org.aspectj.lang.proceedingjoinpoint;/** * 패싯 클래스 : 진입 점 + 알림 * @author 관리자 */public class myaspectxml {/** * 알림 (특정 향상) */public void log () {system.out.println ( "log log"); } / ** * 메소드가 성공적으로 실행되거나 예외가 실행됩니다 * / public void () {System.out.println ( "최종 알림"); } /*** 메소드가 실행 된 후 사후 알림이 실행됩니다. 프로그램에서 예외가 발생하면 사후 알림이 실행되지 않습니다. */ public void attreturn () {System.out.println ( "post unticification"); } / ** * 메소드가 실행 된 후 예외가 있으면 예외 알림이 실행됩니다. } /*** 서라운드 알림 : 메소드가 실행되기 전후에 알림이 이루어집니다. * 기본적으로 대상 객체의 방법을 실행할 수 없으며 대상 객체를 수동으로 실행해야합니다 */ public void (problicejoinpoint joinpoint) {System.out.println ( "랩 알림 1"); // 대상 객체의 메소드가 수동으로 실행하도록하십시오. } catch (Throwable e) {// todo 자동 생성 캐치 블록 e.printstacktrace (); } system.out.println ( "랩 알림 2"); }}5) 구현 클래스 및 패싯 클래스를 주입합니다
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <!-- 配置客户的dao --> <bean id="customerDao"/> <!-- 编写切面类配置好--> <bean id="myAspectXml"/> <!-- 配置AOP --> <aop:config> <!-- 配置切面类:切入点+通知(类型)--> <aop:aspect ref="myAspectXml"> <!-- 配置前置通知,save方法执行之前,增强方法会执行--> <!-- 切入点表达式:execution(public void com.clj.demo5.CustomerDaoImpl.save()) --> <!-- 切入点表达式: 1.execution()固定的,必写2.public可以省略不写3.返回值必写,严格根据切入点方法而定,否则增强方法不会执行,可以用*代替,表示任意的返回值4.包名必写,可以用*代替(如:*..*(默认所有包); com.clj.*) 5.类名必写,可以部分用*(如*DaoImpl表示以'DaoImpl'结尾的持久层实现类),但不建议用*代替整个类名6.方法必写,可以部分用*(如save*表示以'save'开头的方法),但不建议用*代替整个类名7.方法参数根据实际方法而定,可以用'..'表示有0或者多个参数--> <!-- <aop:before method="log" pointcut="execution(public void com.clj.*.CustomerDaoImpl.save(..))"/> --> <aop:before method="log" pointcut="execution(* *..*.*DaoImpl.save*(..))"/> </aop:aspect> </aop:config></beans>
6)测试
package com.clj.demo5;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Demo1 { @Resource(name="customerDao") private CustomerDao customerDao; @Test public void run(){ customerDao.save(); customerDao.update(); }}扩展:切面类升级
<?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:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- bean definitions here --> <bean id="myAspectXml"/> <aop:config> <aop:aspect ref="myAspectXml"> <!-- 配置最终通知<aop:after method="after" pointcut="execution(* *..*.*DaoImpl.save*(..))"/>--> <!-- 配置后置通知<aop:after-returning method="afterReturn" pointcut="execution(* *..*.*DaoImpl.save*(..))"/>--> <!-- 配置异常通知<aop:after-throwing method="afterThrowing" pointcut="execution(* *..*.*DaoImpl.save*(..))"/>--> <aop:around method="around" pointcut="execution(* *..*.*DaoImpl.update*(..))"/> </aop:aspect> </aop:config></beans>
4、Spring框架AOP之注解方式
1)创建接口和实现类
package com.clj.demo1;public interface CustomerDao { public void save(); public void update();} package com.clj.demo1;public class CustomerDaoImpl implements CustomerDao{ @Override public void save() { // TODO Auto-generated method stub System.out.println("Save customer.."); } @Override public void update() { // TODO Auto-generated method stub System.out.println("Update customer"); }}2)定义切面类
package com.clj.demo1;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;/** * 注解方式的切面类* @Aspect表示定义为切面类*/@Aspectpublic class MyAspectAnno { //通知类型:@Before前置通知(切入点的表达式) @Before(value="execution(public * com.clj.demo1.CustomerDaoImpl.save())") public void log(){ System.out.println("记录日志。。"); } //引入切入点@After(value="MyAspectAnno.fun()") public void after(){ System.out.println("执行之后"); } @Around(value="MyAspectAnno.fun()") public void around(ProceedingJoinPoint joinPoint){ System.out.println("环绕通知1"); try { //让目标对象执行joinPoint.proceed(); } catch (Throwable e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("环绕通知2"); } //自定义切入点@Pointcut(value="execution(public * com.clj.demo1.CustomerDaoImpl.save())") public void fun(){ }}3)配置切面类和实现类,并开启自动代理
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 开启自动注解代理--> <aop:aspectj-autoproxy/> <!-- 配置目标对象--> <bean id="customerDao"/> <!-- 配置切面类--> <bean id="myAspectAnno"/></beans>
spring提供了JDBC模板:JdbcTemplate类
1.快速搭建
1)部署环境
这里在原有的jar包基础上,还要添加关乎jdbc的jar包,这里使用的是mysql驱动
2)配置内置连接池,将连接数据库程序交给框架管理,并配置Jdbc模板类
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 先配置连接池(内置) --> <bean id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置JDBC的模板类--> <bean id="jdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean></beans>
3) 테스트
package com.clj.demo2;import java.sql.ResultSet;import java.sql.SQLException;import java.util.List;import javax.annotation.Resource;import org.apache.commons.dbcp.BasicDataSource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.cglib.beans.BeanMap;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;/** * 测试JDBC的模板类,使用IOC的方式* @author Administrator * */@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Demo2 { @Resource(name="jdbcTemplate") private JdbcTemplate jdbcTemplate; /** * 插入*/ @Test public void run1(){ String sql="insert into t_account values(null,?,?)"; jdbcTemplate.update(sql,"李钇林",10000); } /** * 更新*/ @Test public void run2(){ String sql="update t_account set name=? where id=?"; jdbcTemplate.update(sql,"李钇林",1); } /** * 删除*/ @Test public void run3(){ String sql="delete from t_account where id=?"; jdbcTemplate.update(sql,4); } /** * 测试查询,通过主键来查询一条记录*/ @Test public void run4(){ String sql="select * from t_account where id=?"; Account ac=jdbcTemplate.queryForObject(sql, new BeanMapper(),1); System.out.println(ac); } /** * 查询所有*/ @Test public void run5(){ String sql="select * from t_account"; List<Account> ac=jdbcTemplate.query(sql,new BeanMapper()); System.out.println(ac); }}/** * 定义内部类(手动封装数据(一行一行封装数据,用于查询所有) * @author Administrator * */class BeanMapper implements RowMapper<Account>{ @Override public Account mapRow(ResultSet rs, int rowNum) throws SQLException { Account ac=new Account(); ac.setId(rs.getInt("id")); ac.setName(rs.getString("name")); ac.setMoney(rs.getDouble("money")); return ac; } }2、配置开源连接池
一般现在企业都是用一些主流的连接池,如c3p0和dbcp
首先配置dbcp
1)导入dbcp依赖jar包
2)编写配置文件
<!-- 配置DBCP开源连接池--> <bean id="dataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="username" value="root"/> <property name="password" value="root"/> </bean>
将模板类中引入的内置类datasource改为开源连接池的
3)编写测试类
配置c3p0
1)导入c3p0依赖jar包
2)配置c3p0
<!-- 配置C3P0开源连接池--> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean>
将模板类中引入的内置类datasource改为开源连接池的
3)编写测试类
1、什么是事务
数据库事务(Database Transaction) ,是指作为单个逻辑工作单元执行的一系列操作,要么完全地执行,要么完全地不执行。 事务处理可以确保除非事务性单元内的所有操作都成功完成,否则不会永久更新面向数据的资源。通过将一组相关操作组合为一个要么全部成功要么全部失败的单元,可以简化错误恢复并使应用程序更加可靠。一个逻辑工作单元要成为事务,必须满足所谓的ACID(原子性、一致性、隔离性和持久性)属性。事务是数据库运行中的逻辑工作单位,由DBMS中的事务管理子系统负责事务的处理。
2、怎么解决事务安全性问题
读问题解决,设置数据库隔离级别;写问题解决可以使用悲观锁和乐观锁的方式解决
3、快速开发
方式一:调用模板类,将模板注入持久层
1)编写相对应的持久层和也外层,这里省略接口
package com.clj.demo3;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ // 方式一:将jdbc模板类注入到配置文件中,直接在持久层写模板类private JdbcTemplate jdbcTemplate; public void setJdbcTemplate(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public void outMoney(String out, double money) { String sql="update t_account set money=money-? where name=?"; jdbcTemplate().update(sql,money,out); } public void inMoney(String in, double money) { String sql="update t_account set money=money+? where name=?"; jdbcTemplate().update(sql,money,in); } } package com.clj.demo4;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;public class AccountServiceImpl implements AccountService{ //采用的是配置文件注入方式,必须提供set方法private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void pay(String out, String in, double money) { // TODO Auto-generated method stub accountDao.outMoney(out, money); int a=10/0; accountDao.inMoney(in, money); }}2)配置相对应的配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 配置C3P0开源连接池--> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean><!-- 配置JDBC的模板类--> <bean id="jdbcTemplate"> <property name="dataSource" ref="dataSource"/> </bean><!-- 配置业务层和持久层--> <bean id="accountService"> <property name="accountDao" ref="accountDao"/> </bean><bean id="accountDao"> <!-- 注入模板类--> <property name="jdbcTemplate" ref="jdbcTemplate"/> <property name="dataSource" ref="dataSource"/> </bean></beans>
3)测试类
package com.clj.demo3;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Demo1 { @Resource(name="accountService") private AccountService accountService; @Test public void Demo1(){ //调用支付的方法accountService.pay("佳先森","李钇林",100); }}方式二:持久层继承JdbcDaoSupport接口,此接口封装了模板类jdbcTemplate
1)编写配置文件
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置C3P0开源连接池--> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置业务层和持久层--> <bean id="accountService"> <property name="accountDao" ref="accountDao"/> </bean> <bean id="accountDao"> <property name="dataSource" ref="dataSource"/> </bean></beans>
2)更改持久层
package com.clj.demo3;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ //方式一:将jdbc模板类注入到配置文件中,直接在持久层写模板类// private JdbcTemplate jdbcTemplate;// public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {// this.jdbcTemplate = jdbcTemplate;// } //方式二:持久层继承JdbcDaoSupport,它里面封转了模板类,配置文件持久层无需注入模板类,也不需要配置模板类public void outMoney(String out, double money) { //jdbcTemplate.update(psc); String sql="update t_account set money=money-? where name=?"; this.getJdbcTemplate().update(sql,money,out); } public void inMoney(String in, double money) { String sql="update t_account set money=money+? where name=?"; this.getJdbcTemplate().update(sql,money,in); } }3)更改业务层
package com.clj.demo4;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;public class AccountServiceImpl implements AccountService{ //采用的是配置文件注入方式,必须提供set方法private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void pay(String out, String in, double money) { // TODO Auto-generated method stub accountDao.outMoney(out, money); int a=10/0; accountDao.inMoney(in, money); } }4)测试类和上述一样
4、spring事务管理
Spring为了简化事务管理的代码:提供了模板类TransactionTemplate,手动编程的方式来管理事务,只需要使用该模板类即可!!
1、手动编程方式事务(了解原理)
1)快速部署,搭建配置文件,配置事务管理和事务管理模板,并在持久层注入事务管理模板
配置事务管理器
<!-- 配置平台事务管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
配置事务管理模板
<bean id="transactionTemplate"> <property name="transactionManager" ref="transactionManager"/></bean>
将管理模板注入业务层
<bean id="accountService"> <property name="accountDao" ref="accountDao"/> <property name="transactionTemplate" ref="transactionTemplate"/></bean>
全部代码:
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置C3P0开源连接池--> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置业务层和持久层--> <bean id="accountService"> <property name="accountDao" ref="accountDao"/> <property name="transactionTemplate" ref="transactionTemplate"/> </bean> <bean id="accountDao"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 配置平台事务管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 手动编码方式,提供了模板类,使用该类管理事务比较简单--> <bean id="transactionTemplate"> <property name="transactionManager" ref="transactionManager"/> </bean></beans>
2)在业务层使用模板事务管理
package com.clj.demo3;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;public class AccountServiceImpl implements AccountService{ //采用的是配置文件注入方式,必须提供set方法private AccountDao accountDao; //注入事务模板类private TransactionTemplate transactionTemplate; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } public void setTransactionTemplate(TransactionTemplate transactionTemplate) { this.transactionTemplate = transactionTemplate; } /** * 转账的方法*/ public void pay(final String out,final String in, final double money) { transactionTemplate.execute(new TransactionCallbackWithoutResult() { //事务的执行,如果没有问题,提交,如果楚翔异常,回滚protected void doInTransactionWithoutResult(TransactionStatus arg0) { // TODO Auto-generated method stub accountDao.outMoney(out, money); int a=10/0; accountDao.inMoney(in, money); } }); }}3)测试类和上一致
package com.clj.demo4;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext2.xml")public class Demo2 { @Resource(name="accountService") private AccountService accountService; @Test public void Demo1(){ //调用支付的方法accountService.pay("佳先森","李钇林",100); }}申明式事务有两种方式:基于AspectJ的XML方式;基于AspectJ的注解方式
1、XML方式
1)配置配置文件
需要配置平台事务管理
<!-- 配置C3P0开源连接池--> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置平台事务管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
配置事务增强
<tx:advice id="myAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 给方法设置数据库属性(隔离级别,传播行为) --> <!--propagation事务隔离级别:一般采用默认形式:tx:method可以设置多个--> <tx:method name="pay" propagation="REQUIRED"/> </tx:attributes> </tx:advice>
aop切面类
<aop:config> <!-- aop:advisor,是spring框架提供的通知--> <aop:advisor advice-ref="myAdvice" pointcut="execution(public * com.clj.demo4.AccountServiceImpl.pay(..))"/> </aop:config>
全部代码
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置C3P0开源连接池--> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置平台事务管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 申明式事务(采用XML文件的方式) --> <!-- 先配置通知--> <tx:advice id="myAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 给方法设置数据库属性(隔离级别,传播行为) --> <!--propagation事务隔离级别:一般采用默认形式:tx:method可以设置多个--> <tx:method name="pay" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置AOP:如果是自己编写的AOP,使用aop:aspect配置,使用的是Spring框架提供的通知--> <aop:config> <!-- aop:advisor,是spring框架提供的通知--> <aop:advisor advice-ref="myAdvice" pointcut="execution(public * com.clj.demo4.AccountServiceImpl.pay(..))"/> </aop:config> <!-- 配置业务层和持久层--> <bean id="accountService"> <property name="accountDao" ref="accountDao"/> </bean> <bean id="accountDao"> <property name="dataSource" ref="dataSource"/> </bean></beans>
2)编写持久层和业务层(省略接口)
package com.clj.demo5;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ //方式一:将jdbc模板类注入到配置文件中,直接在持久层写模板类// private JdbcTemplate jdbcTemplate;// public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {// this.jdbcTemplate = jdbcTemplate;// } //方式二:持久层继承JdbcDaoSupport,它里面封转了模板类,配置文件持久层无需注入模板类,也不需要配置模板类public void outMoney(String out, double money) { //jdbcTemplate.update(psc); String sql="update t_account set money=money-? where name=?"; this.getJdbcTemplate().update(sql,money,out); } public void inMoney(String in, double money) { String sql="update t_account set money=money+? where name=?"; this.getJdbcTemplate().update(sql,money,in); } } package com.clj.demo5;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.annotation.Transactional;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;public class AccountServiceImpl implements AccountService{ //采用的是配置文件注入方式,必须提供set方法private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void pay(String out, String in, double money) { // TODO Auto-generated method stub accountDao.outMoney(out, money); int a=10/0; accountDao.inMoney(in, money); } }3)测试类
package com.clj.demo4;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext2.xml")public class Demo2 { @Resource(name="accountService") private AccountService accountService; @Test public void Demo1(){ //调用支付的方法accountService.pay("佳先森","李钇林",100); }}2、注解方式
1)配置配置文件
配置事务管理
<bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean>
开启注释事务
<!-- 开启事务的注解--> <tx:annotation-driven transaction-manager="transactionManager"/>
全部代码
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 配置C3P0开源连接池--> <bean id="dataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://192.168.174.130:3306/SSH"/> <property name="user" value="root"/> <property name="password" value="root"/> </bean> <!-- 配置平台事务管理器--> <bean id="transactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <!-- 开启事务的注解--> <tx:annotation-driven transaction-manager="transactionManager"/> <!-- 配置业务层和持久层--> <bean id="accountService"> <property name="accountDao" ref="accountDao"/> </bean> <bean id="accountDao"> <property name="dataSource" ref="dataSource"/> </bean></beans>
2)业务层增加@Transactional
package com.clj.demo5;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.annotation.Transactional;import org.springframework.transaction.support.TransactionCallbackWithoutResult;import org.springframework.transaction.support.TransactionTemplate;//在当前类加此注解表示当前类所有的全部都有事务@Transactionalpublic class AccountServiceImpl implements AccountService{ //采用的是配置文件注入方式,必须提供set方法private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void pay(String out, String in, double money) { // TODO Auto-generated method stub accountDao.outMoney(out, money); int a=10/0; accountDao.inMoney(in, money); } }3)持久层不变
package com.clj.demo5;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{ //方式一:将jdbc模板类注入到配置文件中,直接在持久层写模板类// private JdbcTemplate jdbcTemplate;// public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {// this.jdbcTemplate = jdbcTemplate;// } //方式二:持久层继承JdbcDaoSupport,它里面封转了模板类,配置文件持久层无需注入模板类,也不需要配置模板类public void outMoney(String out, double money) { //jdbcTemplate.update(psc); String sql="update t_account set money=money-? where name=?"; this.getJdbcTemplate().update(sql,money,out); } public void inMoney(String in, double money) { String sql="update t_account set money=money+? where name=?"; this.getJdbcTemplate().update(sql,money,in); } }4)测试类
package com.clj.demo5;import javax.annotation.Resource;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext3.xml")public class Demo3 { @Resource(name="accountService") private AccountService accountService; @Test public void Demo1(){ //调用支付的方法accountService.pay("佳先森","李钇林",100); }}위는이 기사의 모든 내용입니다. 모든 사람의 학습에 도움이되기를 바랍니다. 모든 사람이 wulin.com을 더 지원하기를 바랍니다.