`

spring中Resource和Component注解

 
阅读更多

在使用注解的时候,首先得在配置文件bean.xml中添加命名空间:

xmlns:context="http://www.springframework.org/schema/context"

 

然后在xsi:schemaLocation中添加:

http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd

 

再次引入:

<context:component-scan base-package="com.test"></context:component-scan>

 

它会扫描锁配置的包里面那些类注解了Component

bean.xml

<?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-2.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd">
	
	<context:component-scan base-package="com.test"></context:component-scan>
	
</beans>

 

UserDAOImpl.java

package com.test.dao.impl;

import org.springframework.stereotype.Component;

import com.test.dao.UserDAO;
import com.test.model.User;

@Component("userDAOImpl")
public class UserDAOImpl implements UserDAO {
	public void save(User user) {
		System.out.println("user saved!");
	}
}

 

UserService.java

package com.test.service;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.test.dao.UserDAO;
import com.test.model.User;

@Component("userService")
public class UserService {
	private UserDAO userDAO;

	public void add(User user) {
		userDAO.save(user);
	}

	public UserDAO getUserDAO() {
		return userDAO;
	}

	// 这里会找注解Component的值为:userDAOImpl的那个类
	@Resource(name="userDAOImpl")
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

}

 

注:这里Resource注解要加入common-annotations.jar

分享到:
评论

相关推荐

    Spring注解 - 52注解 - 原稿笔记

    注解包含: 拦截器 , 过滤器 , 序列化 , @After , @AfterReturning , @AfterThrowing , @annotation , @Around , @Aspect , @Autowired , @Bean , @Before , @Component , @ComponentScan , @ComponentScans , @...

    SpringBoot常用注解详解含使用示例(值得珍藏)

    本文将详细介绍Spring Boot中最常用的注解,包括@SpringBootApplication、@Component、@Service、@Repository、@Controller、@RequestMapping、@GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@Autowired...

    spring注解属性的用法

    演示了spring mvc中@Resource,@Component属性的用法

    Spring Boot最常用的30个注解.docx

    详细介绍了Spring Boot最常用的30个注解,包含概念、原理、示例 Spring Boot最常用的30个注解 一、 @SpringBootApplication 二、 Spring Bean 相关 1 @Controller 2 @Service 3 @Repository 4 @Component 5 @Bean 6 ...

    Spring中文帮助文档

    3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一...

    Spring注释 注入方式源码示例,Annotation

    凡带有@Component,@Controller,@Service,@Repository 标志的等于告诉Spring这类将自动产生对象,而@Resource则等于XML配置中的ref,告诉spring此处需要注入对象,所以用@Resource就有了ref的功效。 要用注解注入方式...

    Spring API

    3.12.1. @Component和更多典型化注解 3.12.2. 自动检测组件 3.12.3. 使用过滤器自定义扫描 3.12.4. 自动检测组件的命名 3.12.5. 为自动检测的组件提供一个作用域 3.12.6. 用注解提供限定符元数据 3.13. 注册一...

    基于注解的DI.docx

    常用注解: @Component、创建对象 @Respotory、创建dao对象,用来访问数据库 @Service、创建Service对象,处理...@Resource、jdk中的注解,使用自动注入给引用数据类型赋值,支持byName、byType,默认byName

    springjdbc

    -- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" /&gt; &lt;!-- apache.dbcp连接池的配置 --&gt; ...

    Spring 3 Reference中文

    4.10.1 @Component和更多典型注解 97 4.10.2 自动检测类和bean 的注册. 97 4.10.3 使用过滤器来自定义扫描 98 4.10.4 使用组件定义bean 的元数据.. 99 4.10.5 命名自动检测组件 100 4.10....

    Spring AOP配置源码

    @Component("userService")等价于在spring配置文件中定义一个&lt;bean id="userService"/&gt; @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....

    Spring.html

    注意:使用注解的方式,最终通知和后置通知顺序换了,建议使用环绕通知 注解 配置 声明式事务管理 PlatFormTransactionManager:平台事务管理器:定义了commit/rollback Mybatis/jdbc:...

    spring3.1中文参考文档

    1.3.1.1 Spring依赖和基于Spring ................................................................................................... 14 1.3.1.2 Maven依赖管理 ...............................................

    springboot学习思维笔记.xmind

    @Resource:JSR-250提供的注解 Java配置 @Configuration声明当前类是一个配置类 @Bean注解在方法上,声明当前方法的返回值为一个Bean AOP @Aspect 声明是一个切面 拦截规则@After @Before ...

    SpringMVC-SSH全注解

    注解配置 org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean 配置形式: org.springframework.orm.hibernate3.LocalSessionFactoryBean --&gt; ...

    SpringMVC+Hibernate全注解整合

    &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc=... &lt;mvc:resources mapping="/resource/**" location="/resource/" /&gt; **" location="/jsp/" /&gt; &lt;/beans&gt;

    springmybatis

    MyBatis使用简单的XML或注解用于配置和原始映射,将接口和Java的POJOs(Plan Old Java Objects,普通的Java对象)映射成数据库中的记录. orm工具的基本思想 无论是用过的hibernate,mybatis,你都可以法相他们有一个...

    spring3.2+strut2+hibernate4

    spring3.2+strut2+hibernate4 注解方式。 spring.xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    客户关系管理系统框架搭建(二)

    * 搭建spring和hibernate * 创建beans.xml文件,放置在src下 * 引入命名空间 bean tx context aop &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:context=...

Global site tag (gtag.js) - Google Analytics