目录
2.Spring针对Bean管理中创建对象提供的注解(注解后面都需要value值)
1.什么是注解
①:注解是代码特殊标记,格式:@注解名称(属性名称=属性值,属性名称=属性值…)
②:使用注解,注解作用在类上面,方法上面,属性上边
③:使用注解的目的:简化XML配置
2.Spring针对Bean管理中创建对象提供的注解(注解后面都需要value值)
@Component 普通的类
@Controller 表现层
@Service 业务层
@Repository 持久层
3.用注解的方式创建对象
3.1:编写接口和实现类
public class Car{
public void hello(){
System.out.println("输出成功!!!!");
}
}
3.2: 在需要管理的类上添加@Component注解
@Component("car")
public class Car{
public void hello(){
System.out.println("输出成功!!!!");
}
}
3.3: 编写配置文件,重点是开启注解扫描
<?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">
<!--开启注解扫描 com.qcby所有的包中的所有的类-->
<context:component-scan base-package="com.qcby"/>
</beans>
3.4: 编写测试方法
package com.qcby.test;
import com.qcby.testanno.UserService;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Demo2 {
@Test
public void run3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Car cars = (Car) ac.getBean("www");
cars.hello();
}
}
4.用注解的方实现属性注入(值注入)
@Value 用于注入普通类型(String,int,double等类型)
@Autowired 默认按类型进行自动装配(引用类型)
@Qualifier 不能单独使用必须和@Autowired一起使用,强制使用名称注入
@Resource Java提供的注解,也被支持。使用name属性,按名称注入
具体代码如下
package com.qcby.service;
import com.qcby.Demo;
import com.qcby.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
@Component("car")
//@Controller("www")
public class Car {
@Value("大众")
private String name;
@Value(value = "123456")
private Double money;
// @Autowired
// private User user;
public void hello(){
System.out.println("输出成功!!!!");
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", money=" + money +
// ", user=" + user +
'}';
}
}
@Test
public void run3(){
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
Car cars = (Car) ac.getBean("car");
System.out.println(cars.toString());
}
5.IOC纯注解的方式(全注解<去掉.xml文件>)
纯注解的方式是微服务架构开发的主要方式,所以也是非常的重要。纯注解的目的是替换掉所有的配置文件。但是需要编写配置类
常用的注解总结
@Configuration 声明是配置类
@ComponentScan 扫描具体包结构的
5.1: 编写实体类
package com.qcby.service;
import com.qcby.Demo;
import com.qcby.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
@Component("car")
//@Controller("www")
public class Car {
@Value("大众")
private String name;
@Value(value = "123456")
private Double money;
// @Autowired
// private User user;
public void hello(){
System.out.println("输出成功!!!!");
}
@Override
public String toString() {
return "Car{" +
"name='" + name + '\'' +
", money=" + money +
// ", user=" + user +
'}';
}
}
5.2:编写配置类,替换掉applicationContext.xml配置文件
package com.qcby;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
/*定义为配置类*/
// 扫描指定的包结构
@ComponentScan(value = "com.qcby.service")
public class SpringConfig {
}
5.3: 测试方法的编写
package com.qcby.test;
import com.qcby.demo4.Order;
import com.qcby.demo4.SpringConfig;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@Test
public void run2(){
ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
Car car = (Car) ac.getBean("car");
System.out.println(car.toString());
}
注意这句话:ApplicationContext ac = new AnnotationConfigApplicationContext(SpringConfig.class);
IOC全注解终
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/115383.html