一、基本注解认识与学习
1、@Component : 扫描组件 ,代表被Spring接管了!
相当于这样配置:
2、属性如何注入
@Value(“勒布朗-詹姆斯”) 代表:显示的属性上进行赋值操作。
package com.ZQQQ.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component //组件
@Scope(“prototype”)
public class User {
private String name;
public String getName() {
return name;
}
@Value(“勒布朗-詹姆斯”)
public void setName(String name) {
this.name = name;
}
}
3、@Component //组件
public class User {
private String name;
public String getName() {
return name;
}
@Value("勒布朗-詹姆斯")
public void setName(String name) {
this.name = name;
}
}
4、衍生的注解:
@Component 有几个衍生注解,我们在web开发中,会按照mvc 三层
dao 【@Repository】 // 仓库
Service 【@Service】//组件
controller【@Controller】 //加上此注解代表被Spring 托管了。
这四个注解功能都是一样的,都是代表将某个类注册到spring容器中,装配Bean。
5、@Component //组件
@Scope(“prototype”) //单例模式中的原型模式
6、applicationcontext.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.xsd">
<!--开启基于注解自动装配的支持 -->
<context:annotation-config/>
<bean id="User" class="com.ZQQQ.pojo.User"/>
<!--指定要扫描的包,这个包下的注解就会生效 -->
<context:component-scan base-package="com.ZQQQ"/>
</beans>
7、小结
二、JavaConfig实现配置
pojo类:
package com.ZQQQ.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
@Component
public class User {
private String name;
private int age;
private String sex;
public String getName() {
return name;
}
@Value("科比-布莱恩特")
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@Value("30")
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
@Value("男")
public void setSex(String sex) {
this.sex = sex;
}
}
Config类:也就相当于Applicationcontext.xml 文件
package com.ZQQQ.config;
import com.ZQQQ.pojo.User;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Component;
@Configuration //第一步:等价于 resources 下的ApplicationContext.xml
@Component("com.ZQQQ.pojo")
@Import(ZqConfig02.class)
public class ZqConfig {
@Bean //第三步:注册一个bean,就相当于我们之前写的一个bean标签。
public User getUser(){
/**getUser 相当于bean 标签中的id属性
* 这个方法的返回值,就相当于bean 标签中的class属性
*/
return new User(); //第二步:写成方法的方式 返回需要返回的对象。
}
}
测试类:
import com.ZQQQ.config.ZqConfig;
import com.ZQQQ.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MyTest {
public static void main(String[] args) {
// 如果完全使用了配置类的方式去做,我们只能通过AnnotationConfig 上下文来获取容器,通过配 置类的class对象加载。
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ZqConfig.class);
User user = (User) context.getBean("getUser");
System.out.println(user.getName());
System.out.println(user.getAge());
System.out.println(user.getSex());
}
}
控制台输出:
涉及到的注解总结:
注解 | 含义 |
---|---|
@Component | 这个注解的含义:就是说明这个类被Spring接管了,注册到了容器中。 |
@Value(“科比-布莱恩特”) | 这个注解的含义:显示的注入值 |
@Configuration | 代表这是一个配置类。加上此注解等价于 resources 下的ApplicationContext.xml |
@Bean | 封装成一个Bean容器文件 就是说明这个类被Spring接管了,注册到容器中。因为它本就是@Component |
@Import(ZqConfig02.class) | 引入为一个类,这是涉及到多个Bean也就是类时所用 |
骏马是跑出来的,强兵是打出来的。