Spring_demo2_Annotation_AutoWired

Annotation-based container configuration
5.9.1. @Required 5.9.2. @Autowired 5.9.3. Fine-tuning annotation-based autowiring with qualifiers 5.9.4. Using generics as autowiring qualifiers 5.9.5. CustomAutowireConfigurer 5.9.6. @Resource

5.9.7. @PostConstruct and @PreDestroy


利用spring一步一步搭建起一个例子(本人采用eclipse)




/**
 * Dao主要做数据库的交互工作
 * Modle 是模型 存放你的实体类
 *Service 做相应的业务逻辑处理
 *Action是一个控制器
 * @author syw
 *
 */


modle:实体类

package com.model;

public class User {
	private String username;
	private String password;

	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 Dao 数据库

package com.dao;

import com.model.User;

public interface UserDAO {
	public void saveMySql(User user);
	public void saveOracle(User user);
}


DaoImpl 实现(数据不一样,不一样的实现)

package com.dao.impl;

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

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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

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

public class UserService {
	private UserDAO userDAO;  
	
	public void add(User user) {
		userDAO.saveMySql(user);
	}
	
	public UserDAO getUserDAO() {
		return userDAO;
	}
	
	@Autowired
	//@Qualifier("user2")
	public void setUserDAO(@Qualifier("user1")UserDAO userDAO) {
		this.userDAO = userDAO;
	}
}



service 测试类

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");
		User u = new User();
		u.setUsername("syw");
		u.setPassword("syw");
		service.add(u);
		
	}

}

代码运行:Mysql: syw


源码地址: 点击下载

参考:

Because autowiring by type may lead to multiple candidates, 

it is often necessary to have more control over the selection process. One way to accomplish this is with Spring’s @Qualifier annotation. You can associate qualifier values with specific arguments, narrowing the set of type matches so that a specific bean is chosen for each argument. In the simplest case, this can be a plain descriptive value:


public class MovieRecommender {

    @Autowired
    @Qualifier("main")
    private MovieCatalog movieCatalog;

    // ...

}
The @Qualifier annotation can also be specified on individual constructor arguments or method parameters:

public class MovieRecommender {

    private MovieCatalog movieCatalog;

    private CustomerPreferenceDao customerPreferenceDao;

    @Autowired
    public void prepare(@Qualifier("main")MovieCatalog movieCatalog,
            CustomerPreferenceDao customerPreferenceDao) {
        this.movieCatalog = movieCatalog;
        this.customerPreferenceDao = customerPreferenceDao;
    }

    // ...

}



1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值