首页 > 编程语言 > 详细

Spring入门第十四课

时间:2017-05-24 23:35:50      阅读:323      评论:0      收藏:0      [点我收藏+]

基于注解的方式配置bean(基于注解配置Bean,基于注解来装配Bean的属性)

在classpath中扫描组件

组件扫描(component scanning):Spring能够从classpath下自动扫描,侦测和实例化具有特定注解的组件。

特定组件包括:

-@Component:基本注解,表示了一个受Spring管理的组件

-@Respository:标识持久层组件

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

-@Controller:标识表现层组件

对于扫描到的组件,Spring有默认的命名策略:使用非限定类名,第一个字母小写,也可以在注解中通过value属性值标识组件的名称。

当在组件类上使用了特定的注解之后,还需要在Spring的配置文件中声明<context:component-scan>:

-base-package属性指定一个需要扫描的基类包,Spring容器将会扫描这个基类包里面及其子包中的所有类。

-当需要扫描多个包时,可以使用逗号分隔。

-如果仅希望扫描特定的类而不非基包下的所有类,可以使用resource-pattern属性过滤特定的类,示例:

<context:component-scan
    base-package="com.logan.spring.beans"
    resource-pattern="autowire/*.class"/>

-<context:include-filter>子节点表示要包含的目标类

-<context:exclude-filter>子节点表示要排除在外的目标

-<context:component-scan>下可以拥有若干个<context:include-filter>和<context:exclude-filter>子节点。

看代码

package logan.spring.study.annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}
package logan.spring.study.annotation.repository;

import org.springframework.stereotype.Repository;

@Repository("userRepository")
public class UserRepositoryImpl implements UserRepository {

    @Override
    public void save() {
        // TODO Auto-generated method stub
        System.out.println("UserRepository Save...");

    }

}
package logan.spring.study.annotation.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    
    public void add(){
        System.out.println("UserService add...");
    }
}
package logan.spring.study.annotation.controller;

import org.springframework.stereotype.Controller;

@Controller
public class UserController {
    
    public void execute(){
        System.out.println("UserController execute...");
    }

}

配置文件

<?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"
    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-4.3.xsd">
    
    <context:component-scan base-package="logan.spring.study.annotation"></context:component-scan>

</beans>
package logan.spring.study.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import logan.spring.study.annotation.controller.UserController;
import logan.spring.study.annotation.repository.UserRepository;
import logan.spring.study.annotation.service.UserService;

public class Main {
    public static void main(String[] args) {
        System.out.println(1);
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
        System.out.println(2);
        TestObject to = (TestObject) ctx.getBean("testObject");
        System.out.println(to);
        
        UserController userController = (UserController) ctx.getBean("userController");
        System.out.println(userController);
        
        UserService userService = (UserService) ctx.getBean("userService");
        System.out.println(userService);
        
        UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
        System.out.println(userRepository);
    }

}
package logan.spring.study.annotation.repository;

public interface UserRepository {
    
    void save();

}

 返回的结果为

五月 24, 2017 10:25:12 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@42110406: startup date [Wed May 24 22:25:11 CST 2017]; root of context hierarchy
五月 24, 2017 10:25:12 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-annotation.xml]
logan.spring.study.annotation.TestObject@78ac1102
logan.spring.study.annotation.controller.UserController@2de8284b
logan.spring.study.annotation.service.UserService@396e2f39
logan.spring.study.annotation.repository.UserRepositoryImpl@a74868d

这里使用的jar包有:

技术分享

 可以如下改规则:(通过resource-pattern指定扫描资源)

<?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"
    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-4.3.xsd">
    
    <context:component-scan 
    base-package="logan.spring.study.annotation" 
    resource-pattern="repository/*.class" ></context:component-scan>

</beans>
package logan.spring.study.annotation;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import logan.spring.study.annotation.controller.UserController;
import logan.spring.study.annotation.repository.UserRepository;
import logan.spring.study.annotation.service.UserService;

public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-annotation.xml");
//        TestObject to = (TestObject) ctx.getBean("testObject");
//        System.out.println(to);
        
//        UserController userController = (UserController) ctx.getBean("userController");
//        System.out.println(userController);
        
//        UserService userService = (UserService) ctx.getBean("userService");
//        System.out.println(userService);
        
        UserRepository userRepository = (UserRepository) ctx.getBean("userRepository");
        System.out.println(userRepository);
    }

}

输出的结果为:

五月 24, 2017 10:30:25 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@42110406: startup date [Wed May 24 22:30:25 CST 2017]; root of context hierarchy
五月 24, 2017 10:30:25 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [beans-annotation.xml]
logan.spring.study.annotation.repository.UserRepositoryImpl@32eebfca

<context:include-filter>和<context:exclude-filter>子节点支持多种类型的过滤表达式:

类别 示例 说明
annotation    
assinable    
aspecct    
regex    
custom    

Spring入门第十四课

原文:http://www.cnblogs.com/LoganChen/p/6886183.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!