Spring_demo4_Annotation_Componet

本文详细介绍了Spring框架中各种组件注解的使用方法及意义,包括@Component、@Service、@Repository、@Controller等,并解释了它们之间的区别及如何正确应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考官网

The @Repository annotation is a marker for any class that fulfills the role or stereotype (also known as Data Access Object or DAO) of a repository. Among the uses of this marker is the automatic translation of exceptions as described in Section 15.2.2, “Exception translation”.

Spring provides further stereotype annotations: @Component@Service, and @Controller@Component is a generic stereotype for any Spring-managed component.@Repository@Service, and @Controller are specializations of @Component for more specific use cases, for example, in the persistence, service, and presentation layers, respectively. Therefore, you can annotate your component classes with @Component, but by annotating them with @Repository@Service, or @Controller instead, your classes are more properly suited for processing by tools or associating with aspects. For example, these stereotype annotations make ideal targets for pointcuts. It is also possible that @Repository@Service, and @Controller may carry additional semantics in future releases of the Spring Framework. Thus, if you are choosing between using @Component or @Service for your service layer, @Service is clearly the better choice. Similarly, as stated above, @Repository is already supported as a marker for automatic exception translation in your persistence layer.

5.10.2 Meta-annotations

Many of the annotations provided by Spring can be used as "meta-annotations" in your own code. A meta-annotation is simply an annotation, that can be applied to another annotation. For example, The @Service annotation mentioned above is meta-annotated with with @Component:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component // Spring will see this and treat @Service in the same way as @Component
public @interface Service {

    // ....

}

Meta-annotations can also be combined together to create composed annotations. For example, the @RestController annotation from Spring MVC is composed of@Controller and @ResponseBody.

With the exception of value(), meta-annotated types may redeclare attributes from the source annotation to allow user customization. This can be particularly useful when you want to only expose a subset of the source annotation attributes. For example, here is a custom @Scope annotation that defines session scope, but still allows customization of the proxyMode.



Demo参考
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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
	<context:annotation-config />

	<context:component-scan base-package="com"/>
	
  	<!-- <bean id="userDAO" class="com.dao.impl.UserDaoImpl">
  	</bean>
	
	<<bean id="user2" class="com.dao.impl.UserDaoImpl">
 	 </bean>
 	 
  	 <bean id="userService" class="com.service.UserService">
  	</bean> -->
  	
</beans>

DaoImpl

package com.dao.impl;

import org.springframework.stereotype.Component;

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

@Component("userDao_Componet")
public class UserDaoImpl implements UserDAO{

	@Override
	public void saveMySql(User user) {
		System.out.println("Mysql: " + user.getUsername());
	}

	
	@Override
	public void saveOracle(User user) {
		System.out.println("Oracle: " + user.getPassword());
	}

}

Service

package com.service;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

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

@Component("userService_Componet")
public class UserService {
	private UserDAO userDAO;  
	
	public void add(User user) {
		userDAO.saveMySql(user);
	}
	
	public UserDAO getUserDAO() {
		return userDAO;
	}
	
	/**
	 * @Resource 默认使用userDAO(前提只有一个bean)
	 * @Resource(name = "user2")
	 * @param userDAO
	 */
	/*@Resource*/
	@Resource(name = "userDao_Componet")
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}

testservice

package com.service;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.model.User;

/**
 * Dao主要做数据库的交互工作
 * Modle 是模型 存放你的实体类
 *Service 做相应的业务逻辑处理
 *Action是一个控制器
 * @author tao.zeng
 *
 */
public class UserServiceTest {
	@Test
	public void testAdd() throws Exception {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		
		UserService service = (UserService)ctx.getBean("userService_Componet");
		User u = new User();
		u.setUsername("syw");
		u.setPassword("syw");
		service.add(u);
	}

}







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值