구성 파일
이전 예제에서는 XML Bean 정의를 사용하여 구성 요소를 구성했습니다. 약간 더 큰 프로젝트에는 일반적으로 수백 개의 구성 요소가 있습니다. 이러한 구성 요소가 XML Bean 정의를 사용하여 구성되면 구성 파일의 크기가 높아져 찾아서 유지 관리하는 것이 편리하지 않습니다.
Spring 2.5는 자동 구성 요소 스캔 메커니즘을 소개합니다. 클래스 경로 아래에서 @component, @service, @controller 및 @repository 주석으로 주석이 달린 클래스를 찾을 수 있으며 이러한 클래스를 스프링 컨테이너에 통합하여 관리합니다.
기능은 XML 파일에서 Bean Node 구성 구성 요소를 사용하는 것과 동일합니다. 자동 스캔 메커니즘을 사용하려면 다음 구성 정보를 열어야합니다.
<? 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-4.2.xsd "> <context : component-scan base-package ="cn.itcast "/>>
<context:component-scan base-package="cn.itcast" /> configuration은 <context : annotation-config /> 프로세서를 포함하여 여러 프로세서를 포함하는 여러 프로세서를 구성하여 구성 요소에 등록 된 프로세서를 포함하여 <context:component-scan base-package="cn.itcast" /> 작성되지 않으면 <context:annotation-config/> 해야합니다. <context:annotation-config/> 구성. 또한 기본 패키지는 스캔 해야하는 패키지 (서브 포장)입니다.
주석
@Service는 비즈니스 계층 구성 요소에 주석을 달고 @Controller는 제어 계층 구성 요소 (예 : Struts2의 동작)에 주석을 달고 @Repository는 데이터 액세스 구성 요소, 즉 DAO 구성 요소에 주석을 달아 사용됩니다. @Component는 구성 요소를 나타냅니다. 구성 요소를 분류하기 쉽지 않은 경우이 주석을 사용하여 주석을 달 수 있습니다.
이 기사는 @autowire 주석 및 자동 어셈블리의 경우를 기반으로합니다.
먼저 Spring의 구성 파일을 다음으로 변경합니다.
<? 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-4.2.xsd "> <context : component-scan base-package ="cn.itcast "/>>
예
그런 다음 @Service 주석을 사용하여 다음과 같이 Personservicebean 클래스에 주석을 달 수 있습니다.
@ServicePublic Class Personservicebean은 PersonService {Private Persondao Persondao; 공개 void setpersondao (persondao persondao) {this.persondao = persondao; } @override public void save () {persondao.add (); }}다음과 같이 @repository 주석을 사용하여 Persondaobean 클래스에 주석을 달 수 있습니다.
@RepositoryPublic 클래스 Persondaobean은 persondao {@override public void add () {system.out.println ( "persondaobean에서 add () 메소드를 실행")를 구현합니다. }}마지막으로 스프링 테스트 클래스의 코드를 다음과 같이 수정합니다.
공개 클래스 스프링 테스트 {@test public void instancepring () {acpractApplicationContext ctx = new ClassPathXmlApplicationContext ( "beans.xml"); personservice personservice = (personservice) ctx.getbean ( "personservicebean"); persondao persondao = (persondao) ctx.getbean ( "persondaobean"); System.out.println (personservice); System.out.println (persondao); ctx.close (); }}Instancespring () 메소드를 테스트하면 Eclipse 콘솔이 인쇄된다는 것을 알 수 있습니다.
지정된 이름을 사용하여 가져 오려면 Personservicebean 클래스의 코드를 다음과 같이 수정할 수 있습니다.
@Service ( "personservice") 공공 계급 Personservicebean은 Personservice {private persondao persondao; 공개 void setpersondao (persondao persondao) {this.persondao = persondao; } @override public void save () {persondao.add (); }}이런 식으로 스프링 테스트 클래스의 코드는 다음으로 변경해야합니다.
공개 클래스 스프링 테스트 {@test public void instancepring () {acpractApplicationContext ctx = new ClassPathXmlApplicationContext ( "beans.xml"); personservice personservice = (personservice) ctx.getbean ( "personservice"); System.out.println (personservice); ctx.close (); }}Instancespring () 메소드를 테스트하면 Eclipse 콘솔이 인쇄된다는 것을 알 수 있습니다.
우리는 이전에 스프링 관리 콩의 범위를 배웠으며, 위의 스프링 관리 콩의 범위는 기본적으로 싱글 톤이라는 것을 알 수 있습니다. 물론, 우리는 Personservicebean 클래스의 코드 변경과 같은 스프링 관리 콩의 범위를 다음과 같이 변경할 수 있습니다.
@Service ( "personservice") @Scope ( "프로토 타입") 공개 클래스 personservicebean은 personservice {private persondao persondao; 공개 void setpersondao (persondao persondao) {this.persondao = persondao; } @override public void save () {persondao.add (); }}이것은 Spring이 관리하는 Personservicebean의 범위가 프로토 타입이되었음을 의미합니다. 현재 스프링 테스트 클래스의 코드를 다음과 같이 수정합니다.
공개 클래스 스프링 테스트 {@test public void instancepring () {acpractApplicationContext ctx = new ClassPathXmlApplicationContext ( "beans.xml"); personservice personservice1 = (personservice) ctx.getbean ( "personservice"); personservice personservice2 = (personservice) ctx.getbean ( "personservice"); System.out.println (personservice1 == personservice2); ctx.close (); }}Instancespring () 메소드를 테스트하면 Eclipse 콘솔이 인쇄된다는 것을 알 수 있습니다.
프로토 타입 범위는 스프링 컨테이너에서 콩을 얻을 때마다 새로운 객체라는 것을 의미합니다.
ClassPath 경로에서 자동으로 스캔하여 구성 요소가 스프링 컨테이너에 포함 된 경우 Bean의 초기화 방법 및 파괴 방법을 지정하는 방법은 무엇입니까? 현재 @postconstruct와 @predestroy의 두 가지 주석을 사용해야합니다. 실험을 위해 Personservicebean Class의 코드를 다음과 같이 수정했습니다.
@Service ( "personservice") 공공 계급 Personservicebean은 Personservice {private persondao persondao; @PostConstruct public void init () {System.out.println ( "초기 리소스"); } @predestroy public void destroy () {System.out.println ( "Destroy, Close Resource"); } public void setpersondao (persondao persondao) {this.persondao = persondao; } @override public void save () {persondao.add (); }}다음으로 스프링 테스트 클래스 코드를 다음과 같이 수정해야합니다.
공개 클래스 스프링 테스트 {@test public void instancepring () {acpractApplicationContext ctx = new ClassPathXmlApplicationContext ( "beans.xml"); personservice personservice = (personservice) ctx.getbean ( "personservice"); ctx.close (); }}이러한 방식으로 Instancespring () 메소드를 테스트하면 Eclipse 콘솔이 인쇄됩니다.
소스 코드를 보려면 클릭하여 스프링을 자동으로 스캔하고 Bean을 관리 할 수 있도록합니다.
요약
위는 편집기가 소개 한 자동 스프링 스캔 패키지입니다. 모든 사람에게 도움이되기를 바랍니다. 궁금한 점이 있으면 메시지를 남겨 주시면 편집자가 제 시간에 모든 사람에게 답장을 드리겠습니다. Wulin.com 웹 사이트를 지원해 주셔서 대단히 감사합니다!