Spring框架bean的配置(3):基于注解的配置

时间:2022-07-14 19:52:23

1.基于注解的配置:

@Component: 基本注解, 标识了一个受 Spring 管理的组件

@Respository: 标识持久层组件

@Service: 标识服务层(业务层)组件

@Controller: 标识表现层组件

将这些架包放入在工程目录下建立的lib文件夹里,并解压

commons-logging-1.1.1

spring-aop-4.0.0.RELEASE

spring-beans-4.0.0.RELEASE

spring-context-4.0.0.RELEASE

spring-core-4.0.0.RELEASE

spring-expression-4.0.0.RELEASE

---------------------------------------------------------------------------

建立接口:UserRepository

package com.atguigu.spring.beans.annotation.test;

public interface UserRepository {
void save();
}

建立类:UserRepositoryImpl继承于接口:UserRepository

package com.atguigu.spring.beans.annotation.test;

import org.springframework.stereotype.Repository;

@Repository("userRepository") //标识持久层组件
public class UserRepositoryImpl implements UserRepository { public void save() {
System.out.println("panpan123");
}
}

建立类:UserService

package com.atguigu.spring.beans.annotation.test;

import org.springframework.stereotype.Service;

@Service  //标识服务层(业务层)组件
public class UserService {
public void add(){
System.out.println("panpan456");
}
}

建立类:UserController

package com.atguigu.spring.beans.annotation.test;

import org.springframework.stereotype.Controller;

@Controller  //标识表现层组件
public class UserController {
public void test(){
System.out.println("panpan789");
}
}

上述类都是建立在包com.atguigu.spring.beans.annotation.test之下;

spring的xml配置文件:beansannotation.xml,在com.atguigu.spring.beans.annotation.test包或子包 下带有上述四个注解的,可以被IOC容器识别

<context:component-scan  base-package="com.atguigu.spring.beans.annotation.test" >
</context:component-scan>

加入resource-pattern="repository/*.class,是指识别repository注解的类;

<context:component-scan   base-package="com.atguigu.spring.beans.annotation.test"  resource-pattern="repository/*.class">
</context:component-scan>

注解过滤:指不包含 类型是 注解,导入的包(import后面的)是org.springframework.stereotype.Repository的类;

<context:component-scan
base-package="com.atguigu.spring.beans.annotation"
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

取消默认的use-default-filters="false",包含...

<context:component-scan
base-package="com.atguigu.spring.beans.annotation" use-default-filters="false">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

类过滤:type="assignable",不包含全类名是com.atguigu.spring.beans.annotation.test.UserRepository

<context:component-scan
base-package="com.atguigu.spring.beans.annotation"
<context:exclude-filter type="assignable" expression="com.atguigu.spring.beans.annotation.test.UserRepository"/>
</context:component-scan>

类过滤:type="assignable",取消默认的use-default-filters="false",只包含全类名为com.atguigu.spring.beans.annotation.test.UserRepositoryImpl类

<context:component-scan
base-package="com.atguigu.spring.beans.annotation" use-default-filters="false">
<context:exclude-filter type="assignable" expression="com.atguigu.spring.beans.annotation.test.UserRepositoryImpl"/>
</context:component-scan>

建立Main类,进行测试:

package com.atguigu.spring.beans.annotation.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx=new ClassPathXmlApplicationContext("beansannotation.xml"); TestObject test=(TestObject) ctx.getBean("testObject");
System.out.println(test); UserController controller=(UserController) ctx.getBean("userController");
System.out.println(controller); UserService service=(UserService) ctx.getBean("userService");
System.out.println(service); UserRepositoryImpl repository=(UserRepositoryImpl) ctx.getBean("userRepository");
System.out.println(repository);
} }