什么是注解?
1.注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值…)
2.使用注解,注解作用在类上面,方法上面,属性上面
3.使用注解目的:简化xml配置
Spring 针对 Bean管理中创建对象提供注解
1.@Component
2.@Service
3.@Controller
4.@Repository
上面四个注解功能是一样的,都可以用来创建bean实例
基于注解方式实现对象创建
一、引入依赖(spring-aop)
二、开启组件扫描
在配置文件中引入一个名称空间context
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">
<!-- 开启组件扫描
1 如果扫描多个包,多个包之间使用逗号隔开
2 扫描包上层目录。com下面的所有包
-->
<context:component-scan base-package="com.zhujie,com.service"></context:component-scan>
三、创建类,在类上面添加创建对象注解
package com.service;
import org.springframework.stereotype.Component;
//注解里面value属性值可以省略不写,默认为类名称首字母小写
@Component(value = "userService") //<bean id="userService" class=".."></bean>
public class UserService {
public void add(){
System.out.println("service add...");
}
}
测试类
package com.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestService {
@Test
public void testUserService(){
ApplicationContext applicationContext = new
ClassPathXmlApplicationContext("bean7.xml");
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.add();
}
}
组件扫描补充:
<!--
use-default-filters="false" 表示现在不使用默认filter,自己配置filter
context:include-filter 设置扫描哪些内容,例如只扫描@Controller
-->
<context:component-scan base-package="com.zhujie" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--
下面配置扫描包所有内容
context:exclude-filter 设置不扫描哪些内容,例如不扫描@Controller
-->
<context:component-scan base-package="com.service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
基于注解方式实现属性注入
1.@Autowired
根据属性类型进行自动装配
2.@Qualifier
根据属性名称进行注入
3.@Resource
可以根据类型注入,可以根据名称注入
4.@Value
注入普通类型属性
@Autowired注解演示
UserP接口
package com.service;
public interface UserP {
public void pdd();
}
UserP实现类UserImpl
package com.service;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
@Repository//创建对象
public class UserImpl implements UserP {
@Override
public void pdd() {
System.out.println("pdddd...");
}
}
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class UserService {
/*
不需要添加set方法
添加注入属性注解
根据类型注入属性,根据UserP类型找到实现类
*/
@Autowired
private UserP userP;
public void add() {
System.out.println("service add...");
userP.pdd();
}
}
@Qualifier注解的使用,和@Autowired一起搭配
当接口中有多个实现类的时候,需要指定名称
UserP实现类UserImpl
package com.service;
import org.springframework.stereotype.Repository;
import javax.annotation.Resource;
@Repository(value = "userImpl1")
public class UserImpl implements UserP {
@Override
public void pdd() {
System.out.println("pdddd...");
}
}
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Autowired
@Qualifier(value = "userImpl1")//根据名称进行注入
private UserP userP;
public void add() {
System.out.println("service add...");
userP.pdd();
}
}
@Resource注解演示
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class UserService {
//@Resource 根据类型注入
@Resource(name = "userImpl1")//根据名称注入
private UserP userP;
public void add() {
System.out.println("service add...");
userP.pdd();
}
}
@Value注解演示
package com.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
@Service
public class UserService {
@Value(value = "abc")
private String name;
public void add() {
System.out.println("service add..."+name);
}
}
完全注解开发
1.创建配置类,代替xml配置文件
package com.service;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration //作为配置类,替代xml配置文件
@ComponentScan(basePackages = {"com.service"})//开启扫描
public class SpringConfig {
}
测试类
package com.service;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestService {
@Test
public void testUserService2(){
//加载配置类
ApplicationContext applicationContext = new
AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = applicationContext.getBean("userService", UserService.class);
userService.add();
}
}