码迷,mamicode.com
首页 > 编程语言 > 详细

Spring使用教程(三)注解1

时间:2015-06-10 00:54:00      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

 

1:@component:基本注解,标识了一个受Spring管理的主键

2:@respository:标识持久层主键

3:@Service:标识服务层(业务层)主键

4:@Controller:标识表示层主键

Spring 有默认的命名策略: 使用非限定类名, 第一个字母小写. 也可以在注解中通过 value 属性值标识组件的名称
 
使用注解,需要在Spring的配置文件中声明:<context:component-scan>
base-package 属性指定一个需要扫描的基类包Spring 容器将会扫描这个基类包里及其子包中的所有类.
当需要扫描多个包时, 可以使用逗号分隔.
 
如果仅希望扫描特定的类而非基包下的所有类,可使用 resource-pattern 属性过滤特定的类
<context:include-filter> 子节点表示要包含的目标类  需要在<context:component-scan>配置use-default-filters="false"
<context:exclude-filter> 子节点表示要排除在外的目标类
–<context:component-scan> 下可以拥有若干个 <context:include-filter> 和 <context:exclude-filter> 子节点
 
技术分享
<?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.0.xsd">
        <!-- 指定Spring IOC容器扫描的包 -->
        <!-- resource-pattern  扫描指定的资源 -->
        <!--  <context:component-scan base-package="com.test.spring.annotation"
            resource-pattern="controller/*.class"
        ></context:component-scan>-->
        
        <!-- exclude-filter 子节点指定排除那些指定表达式的组件 -->
        <!-- include-filter 子节点包含那些表达式的组件,需要配置 use-default-filters为false-->
        <context:component-scan base-package="com.test.spring.annotation" use-default-filters="false"> 
        <!-- 
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
            <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
             -->
             <!-- - 
             <context:exclude-filter type="assignable" expression="com.test.spring.annotation.TestObject"/>
             -->
             <context:include-filter type="assignable" expression="com.test.spring.annotation.TestObject"/>
        </context:component-scan>
</beans>
View Code
技术分享
package com.test.spring.annotation;

import org.springframework.stereotype.Component;

@Component
public class TestObject {

}



package com.test.spring.annotation.controller;

import org.springframework.stereotype.Controller;

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


package com.test.spring.annotation.repository;

public interface UserRepository {
    public void save();
}



package com.test.spring.annotation.repository;

import org.springframework.stereotype.Repository;
//注解可以通过value属性值标识,bean在IOC容器中的name 
@Repository("userRepository")
public class UserRepositoryImlpl implements UserRepository{

    public void save() {
        System.out.println("实现接口的save");
    }

     
}




package com.test.spring.annotation.service;

import org.springframework.stereotype.Service;

@Service
public class UserService {
    public void add(){
        System.out.println("USERSERVICE的 add");
    }
}
View Code
package com.test.spring.annotation;

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

import com.test.spring.annotation.controller.UserController;
import com.test.spring.annotation.repository.UserRepository;
import com.test.spring.annotation.repository.UserRepositoryImlpl;
import com.test.spring.annotation.service.UserService;

public class Main {
    public static void main(String[] args) {
        ApplicationContext con=new ClassPathXmlApplicationContext("beans_annocation.xml");
        TestObject to=(TestObject) con.getBean("testObject");
        System.out.println(to);
        
        UserService ser=(UserService) con.getBean("userService");
        System.out.println(ser);
        
        UserController ucon=(UserController) con.getBean("userController");
        System.out.println(ucon);
        
        UserRepositoryImlpl ur=(UserRepositoryImlpl) con.getBean("userRepository");
        System.out.println(ur);
        
    }
}

 

Spring使用教程(三)注解1

标签:

原文地址:http://www.cnblogs.com/img-zoom/p/4558826.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!