初识 Spring(05)---(Annotation注解)

Annotation 注解

注入注解:@Autowired 注入

1.加在set方法上, 默认按类型by type,如果在 spring 容器中能够找到多个相同的类型,就从这多个类型找查找有没有一个key的值跟我们的 set 方法的参数名相同,如果有,就注入;没有的话,就无法判断要注入哪一个,导致报错

如果想用byName,使用@Qulifie 需要写在参数上

文件目录

代码:配置文件: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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
		<context:component-scan base-package="com.neudeu.service,com.neuedu.dao"></context:component-scan>
	
</beans>

base-package="com.neudeu.service,com.neuedu.dao  扫描这两个包

Test.java

package com.neuedu.test;


import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
	
	}
 
}

UserDao.java

package com.neuedu.dao;

import org.springframework.stereotype.Component;
  
@Component   //注解方式
//Spring 自动地给这个类创建对象并将这个类放到 String 容器中去

public class UserDao {
	public UserDao() {
		System.out.println("UserDao...");
	}
	public void save(){
		System.out.println("保存用户信息...");
	}
}

@Component   //注解方式

UserService.java

package com.neudeu.service;

import org.springframework.stereotype.Component;

import com.neuedu.dao.UserDao;
@Component 
public class UserService {
	public UserService() {
		System.out.println("UserService...");
	}
	private UserDao userDao;
	public void save(){
		userDao.save();
	}
} 

输出:

修改代码:
UserService.java

package com.neudeu.service;

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

import com.neuedu.dao.UserDao;
@Component 
public class UserService {
	public UserService() {
		System.out.println("UserService...");
	}
	private UserDao userDao;
	public void save(){
		userDao.save();
	}
	@Autowired  //自动的将 userDao的属性注入进来   //修改代码
	public void setUserDao(UserDao userDao) {    //修改代码
		System.out.println("setUserDao...");     //修改代码
		this.userDao = userDao;                    //修改代码
	}
} 

输出:    @Autowired  //自动的将 userDao的属性注入进来

修改代码:

Test.java

package com.neuedu.test;


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

import com.neudeu.service.UserService;
public class Test {

	public static void main(String[] args) {
		
		ApplicationContext ac = new ClassPathXmlApplicationContext("beans.xml");
		UserService userService = ac.getBean(UserService.class);
		userService.save();
	}
 
}

输出:

set 注入 byName,按名称注入

修改代码:配置文件: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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
		<context:component-scan base-package="com.neudeu.service,com.neuedu.dao"></context:component-scan>
		<bean id="ud" class="com.neuedu.dao.UserDao"></bean>
</beans>

输出:byName,按名称注入

 

修改代码: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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
		<context:component-scan base-package="com.neudeu.service,com.neuedu.dao"></context:component-scan>
		<bean id="ud" class="com.neuedu.dao.UserDao"></bean>
		<bean id="ud1" class="com.neuedu.dao.UserDao"></bean>
    //修改代码
</beans>

UserService.java

package com.neudeu.service;

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

import com.neuedu.dao.UserDao;
@Component 
public class UserService {
	public UserService() {
		System.out.println("UserService...");
	}
	private UserDao userDao;
	public void save(){
		userDao.save();
	}
	@Autowired  //自动的将 userDao的属性注入进来
	
	public void setUserDao(@Qualifier("ud1") UserDao userDao) {  //修改代码
		System.out.println("setUserDao...");
		this.userDao = userDao;
	}
} 

输出:

2.加在属性上,默认按类型bytype; 如果想用byName,使用@Qulifie 

修改代码: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.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

	
		<context:component-scan base-package="com.neudeu.service,com.neuedu.dao"></context:component-scan>
		<bean id="ud" class="com.neuedu.dao.UserDao"></bean>
		<bean id="userDao" class="com.neuedu.dao.UserDao"></bean>  //修改代码
</beans>

UserService.java

package com.neudeu.service;

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

import com.neuedu.dao.UserDao;
@Component 
public class UserService {
	@Autowired  //自动的将 userDao的属性注入进来
	@Qualifier("ud")
	private UserDao userDao;
	public UserService() {
		System.out.println("UserService...");
	}
	
	public void save(){
		userDao.save();
	}
	
} 

输出:

@Resource 注入

 默认按名称,名称找不到,按类型;可以指定特定名称

修改代码:

UserService.java

package com.neudeu.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.neuedu.dao.UserDao;
@Component 
public class UserService {
	@Resource //修改代码
	@Qualifier("ud")
	private UserDao userDao;
	public UserService() {
		System.out.println("UserService...");
	}
	
	public void save(){
		userDao.save();
	}
	
} 

输出:@Resource 作用和 Autowired 一样,不过注入方式不同,前者默认按名称byName,后者默认按类型bytype

强制按名称,找不到则报错

UserService.java

package com.neudeu.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.neuedu.dao.UserDao;
@Component 
public class UserService {
	//@Autowired  //自动的将 userDao的属性注入进来
	@Resource(name="ud1")  //修改代码
	//@Qualifier("ud")
	private UserDao userDao;
	public UserService() {
		System.out.println("UserService...");
	}
	
	public void save(){
		userDao.save();
	}
	
} 

输出:

 组件注解

 @Component,@Controller,@Service,@Repository

初始化的名字默认为类名首字母小写,可以指定初始化bean的名字;

实质没有任何区别,功能都一样,在哪个层都可以随意调用,但推荐尽量遵从规范。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值