Spring ---annotation (重点)--Resource, Component 重要!!!

beans.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:annotation-config />
	<context:component-scan base-package="com.bjsxt"/>
</beans>

UserServiceTest.java:

package com.bjsxt.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.model.User;

//Dependency Injection
//Inverse of Control
public class UserServiceTest {

	@Test
	public void testAdd() throws Exception {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		UserService service = (UserService)ctx.getBean("userService");
		service.add(new User());
		ctx.destroy();
	}
}

 

UserService.java:

package com.bjsxt.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.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;

@Component("userService")
public class UserService {
	private UserDAO userDAO;
	public void init() {
		System.out.println("init");
	}
	public void add(User user) {
		userDAO.save(user);
	}
	public UserDAO getUserDAO() {
		return userDAO;
	}

	@Resource(name="u")
	public void setUserDAO( UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	public void destroy() {
		System.out.println("destroy");
	}
}

UserDAOImpl.java:

package com.bjsxt.dao.impl;
import org.springframework.stereotype.Component;
import com.bjsxt.dao.UserDAO;
import com.bjsxt.model.User;
@Component("u")
public class UserDAOImpl implements UserDAO {
	public void save(User user) {
		//Hibernate
		//JDBC
		//XML
		//NetWork
		System.out.println("user saved!");
	}
}

  

 

  

  

时间: 2024-08-29 18:44:39

Spring ---annotation (重点)--Resource, Component 重要!!!的相关文章

Spring ---annotation (重点)--AutoWired 不常用

1. 默认按类型 by type, 如果想用byname, 使用@Qualifier 2. 如果写在set上, @qualifier需要写在参数上 bean.xml: 默认bytype去找set方法, 注入 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=&quo

spring annotation功能备注

  @Autowired @Autowired 注释可以在 setter 方法中被用于自动连接 bean.以type方式进行匹配. 一个构造函数 @Autowired 说明当创建 bean 时,即使在 XML 文件中没有使用 元素配置 bean ,构造函数也会被自动连接.   映射方式1    对变量使用@Autowired,在xml中注入其他bean,可以在注入对象中省略配置类的成员变量 映射方式2    对set方法使用@Autowired,在xml中注入其他bean,可以在注入对象中省略配

spring下应用@Resource, @Autowired 和 @Inject注解进行依赖注入的差

代码: 为了探寻  '@Resource', '@Autowired', 和'@Inject'如何解决依赖注入中的问题,我创建了一个"Party"接口,和它的两个实现类"Person","Organization".这样我就可以在注入Bean的时候不必使用具体类型(指使用接口类型即可).这样做也方便我研究当一个接口有多个实现类与之匹配的时候Spring是如何解决依赖注入的不确定性的. public interface Party {} packa

spring Annotation 组件注入

spring 注解的分类 启动spring自己主动扫描功能 <context:component-scan/> [email protected]: 它用于将数据訪问层 (DAO 层 ) 的类标识为 Spring Bean.详细仅仅需将该注解标注在 DAO 类上就可以. 为什么 @Repository 仅仅能标注在 DAO 类上呢? 这是由于该注解的作用不仅仅是将类识别为 Bean,同一时候它还能将所标注的类中抛出的数据訪问异常封装为 Spring 的数据訪问异常类型. Spring 本身提供

【转载】Spring加载resource时classpath*:与classpath:的区别

免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:kyfxbl     原文地址: spring配置中classpath和classpath*的区别   在spring配置文件里,可以用classpath:前缀,来从classpath中加载资源  比如在src下有一个jdbc.properties的文件,可以用如下方法加载: <bean id="propertyConfigurer" class="

[asp.net webfrom+spring.net Error] Resource handler for the &#39;web&#39; protocol is not defined.

错误提示: Resource handler for the 'web' protocol is not defined. 解决方案: 修改web.config如下: <system.web> <httpHandlers> <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web"/> </htt

Spring Annotation(@resource、@component)

1 package com.bxw.dao.impl; 2 3 import org.springframework.stereotype.Component; 4 5 import com.bxw.dao.UserDao; 6 import com.bxw.po.User; 7 8 @Component("userDao") 9 public class UserDaoImpl implements UserDao { 10 public void save(User u){ 11

Spring - Annotation Based Configuration

Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the re

Spring Annotation注解进行aop的学习

使用Maven管理项目,pom文件为: 1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 2 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd&