Spring基础学习之使用注解开发

前言

小伙伴们,大家好,我是狂奔の蜗牛rz,当然你们可以叫我蜗牛君,我是一个学习Java半年多时间的小菜鸟,同时还有一个伟大的梦想,那就是有朝一日,成为一个优秀的Java架构师。
这个Spring基础学习系列是用来记录我学习Spring框架基础知识的全过程 (这个系列是参照B站狂神的Spring5最新教程来写的,由于是之前整理的,但当时没有发布出来,所以有些地方可能有错误,希望大家能够及时指正!)
之后我将会以一天一更的速度更新这个系列,还没有学习Spring5框架的小伙伴可以参照我的博客学习一下;当然学习过的小伙伴,也可以顺便跟我一起复习一下基础。
最后,希望能够和大家一同进步吧!加油吧!少年们!
废话不多说,让我们开始今天的学习内容吧,今天我们来到了Spring基础学习的第八站:使用注解开发

8.使用注解开发

8.1 注解开发前提

8.1.1 导入spring-aop的jar包

  • 在Spring4之后,要使用注解开发,必须要保证aop的包导入了

在这里插入图片描述

8.1.2 开启注解的支持

  • 使用注解需要在applicationContext.xml文件中导入context约束,增加注解的支持
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
</beans>

8.2 使用@Component和@Value注解

8.2.1 使用配置文件注入信息

1.编写User实体类
package com.kuang.pojo;
// 使用@Component注解, 表示将该类作为组件注入到Spring容器中
@Component 
// 创建User实体类
public class User {
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
2.编写bean的xml配置文件
  • 在resources文件下创建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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
     <!-- 注入User实体类的bean信息 -->
    <bean id="user" class="com.kuang.pojo.User">
    	<property name="name" value="唐三"></property>
    </bean>
</beans>
3.编写UserDao接口类
package com.kuang.dao;

// 使用@Repository注解, 将该类标注为Dao层, 将其交由Spring的IOC的容器进行管理
@Repository 
public class UserDao {
}

4.编写UserService服务类
package com.kuang.service;

// 使用@Service注解, 将该类标注为Service层, 将其交由Spring的IOC的容器进行管理
@Service 
public class UserService {
}
5.编写UserController控制类
package com.kuang.controller;

// 使用@Controller注解, 将该类标注为Controller, 将其交由Spring的IOC容器进行管理
@Controller
public class UserController {
}
6.编写MyTest测试类
import com.kuang.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        // 获取ApplicationContext:拿到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过IOC容器获取bean信息
        User user = (User) context.getBean("user");
        // 打印用户名字信息
        System.out.println(user.name);
    }
}
7.测试结果

在这里插入图片描述

8.2.2 使用@Vaule注解注入信息

1.修改User实体类
1-1 在属性前面使用@Vaule注解
package com.kuang.pojo;
public class User {
    // 使用@Value注解:给属性赋值
    @Value("唐昊") 
    public  String name;
}
1-2 在set方法前使用@Value注解
package com.kuang.pojo;
public class User {
    public  String name;
    // 使用@Value注解:给属性赋值
    @Value("唐昊") 
    public void setName(String name) {
        this.name = name;
    }

}
2.修改applicationContext.xml文件
  • 在配置文件中注入User实体类的bean信息时,不设置其属性信息
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
     <!-- 注入User实体类的bean信息,但不设置其属性信息 -->
    <bean id="user" class="com.kuang.pojo.User">
    </bean>
</beans>
3.编写测试类
import com.kuang.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        //获取ApplicationContext:拿到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        //通过IOC容器获取bean信息
        User user = (User) context.getBean("user");
        //打印用户名字信息
        System.out.println(user.name);
    }
}
4.测试结果

在这里插入图片描述

结果查询名为唐昊的用户成功!

5.测试结论
  • 在User实体类的name属性前使用@Value(“唐三”) 注解,等价于 在beans的xml配置文件中使用 property标签,设置name属性值为"name",value属性值为"唐三"
  • @Vaule属性不仅可以使用在实体类的属性前面,也可以在对应属性的set方法前使用

8.2.3 使用@Component注解注入信息

1.修改User实体类
package com.kuang.pojo;
// 使用@Component注解, 表示将该类作为组件注入到Spring容器中
@Component  
public class User {
    // 使用@Value注解:给属性赋值
    @Value("唐三") 
    public  String name;
}
2.修改applicationContext.xml文件
  • 删除User实体类的bean信息注入
<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
</beans>
3.编写测试类
import com.kuang.pojo.User;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MyTest {
    public static void main(String[] args) {
        // 获取ApplicationContext:拿到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过IOC容器获取bean信息
        User user = (User) context.getBean("user");
        // 打印用户名字信息
        System.out.println(user.name);
    }
}
4.测试结果

在这里插入图片描述

报错No bean named ‘user’ available

原因找不到name为user的Bean信息

解决方法

在applicationContext.xml文件中指定要扫描的包

5.解决没有名为user的Bean信息问题
  • 再次修改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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
    <!-- 指定要扫描的包,这个包下的注解就会生效 -->
    <context:component-scan base-package="com.kuang"/>
</beans>
6.修改后测试结果

在这里插入图片描述

结果查询名为小舞的用户成功!

7.测试结论

在User实体类前面使用@Component注解,作用与在bean的xml配置文件中写
<bean id="user" class="com.kuang.pojo.User"></bean>相同

8.3 衍生的注解

@Component 有几个衍生注解,我们在web开发中,会按照MVC三层架构分层

  • dao【@Repository】
  • service【@Service】
  • controller【@Controller】

8.3.1 UserDao接口类中使用@Repository注解

  • 修改UserDao接口类,使用@Repository注解实现
package com.kuang.dao;

// 使用@Repository注解, 将该类标注为Dao层, 将其交由Spring的IOC的容器进行管理
@Repository 
public class UserDao {
}

8.3.2 使用@Service注解

  • 修改UserService服务类,使用@Service注解实现
package com.kuang.service;

// 使用@Service注解, 将该类标注为Service层, 将其交由Spring的IOC的容器进行管理
@Service
public class UserService {
}

8.3.3 使用@Controller注解

  • 修改UserController控制类,使用@Controller注解实现
package com.kuang.controller;

// 使用@Controller注解, 将该类标注为Controller, 将其交由Spring的IOC容器进行管理
@Controller
public class UserController {
}

8.3.4 使用结论

这四个注解功能都是一样的,都是代表将某个类注册到Spring中,装配Bean

8.4 自动装配

@Autowired自动装配通过类型,名字
@Qualifier如果@Autowired不能唯一自动装配上属性,则需要通过@Qualifier(value=“xxx”)
@Resource自动装配通过名字,类型

8.4.1 使用注解自动装配的前提

首先要在对应的beans配置文件中导入context约束和开启注解的支持

1.导入context约束
  • 在applicationContext.xml配置文件的beans标签中插入如下的context约束
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
  • 插入约束后,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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
</beans>
2.开启配置注解的支持
  • 在applicationContext.xml配置文件中引入如下代码
<!--开启注解的支持-->
<context:annotation-config/>
  • 引入注解支持后,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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解的支持-->
    <context:annotation-config/>
</beans>

8.4.2 @Autowired注解的使用

1.编写People实体类
1-1 在属性上使用
public class People {
    @Autowired //在属性上使用@Autowired自动装配
    private Cat cat;
    @Autowired //在属性上使用@Autowired自动装配
    private  Dog dog;
    private String name;
    public Cat getCat() {
        return cat;
    }
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    public Dog getDog() {
        return dog;
    }
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
1-2 在set方法上使用
public class People {
    private Cat cat;
    private  Dog dog;
    private String name;
    @Autowired //在set方法上使用@Autowired自动装配
    public void setCat(Cat cat) {
        this.cat = cat;
    }
    @Autowired //在set方法上使用@Autowired自动装配
    public void setDog(Dog dog) {
        this.dog = dog;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
1-3 不编写set方法
public class People {
    @Autowired
    private Cat cat;
    @Autowired
    private  Dog dog;
    private String name;
    public void setName(String name) {
        this.name = name;
    }
    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
2.编写applicationContext.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!--注意:一定导入context约束-->
<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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!--注意:一定要开启注解的支持!-->
    <context:annotation-config/>
    <!--编写Cat实体类的Bean信息-->
	<bean id="cat" class="com.kuang.pojo.Cat"></bean>
    <!--编写Dog实体类的Bean信息-->
    <bean id="dog" class="com.kuang.pojo.Dog"></bean>
    <!--编写People实体类的Bean信息-->
    <bean id="people" class="com.kuang.pojo.People">
        <!--给name属性设置值-->
        <property name="name" value="华农兄弟"></property>
    </bean>
</beans>       
3.编写MyTest测试类
public class MyTest {
    @Test
    public void test2(){
        //获取上下文信息,得到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
        //从IOC容器中获取Bean信息:声明类型后就不需要强制转换了
        People people = context.getBean("people", People.class);
        //主人让宠物狗叫
        people.getDog().shout();
        //主人让宠物猫叫
        people.getCat().shout();
    }
}
4.测试结果
4-1 在属性上使用

在这里插入图片描述

结果成功输出了猫叫和狗叫,“汪汪汪”和“喵呜“!

4-2 在set方法上使用

在这里插入图片描述

结果与4-1的测试结果相同!

4-2 不编写set方法

在这里插入图片描述

结果与4-1的测试结果相同!

5.@Autowired注解使用总结
  • 直接在属性上使用即可,也可以在set方法上使用,甚至可以不写set方法!
  • 使用Autowired虽然可以不编Set方法了,但前提是这个自动装配的属性在IOC(Spring)容器中存在,且符合byName的使用规则 (要引用的Bean的id和当前设置自动装配的Bean的set方法的值要一致)
  • 默认是按照类型(byType)装配依赖对象,如果想使用按照名称(byName)来装配,可以结合@qualifier注解使用

8.4.3 @Qualifier注解的使用

如果@Autowired自动装配的环境中比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value = “xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!

1.修改People实体类
public class People {
    @Autowired
    //指定一个唯一的bean对象cat11注入
    @Qualifier(value = "cat11")
    private Cat cat;
    @Autowired
    //指定一个唯一的bean对象dog222注入
    @Qualifier(value = "dog222")
    private  Dog dog;
    private String name;
    public People(@Nullable String name) {
        this.name = name;
    }
    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
2.修改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
           https://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解的支持-->
    <context:annotation-config/>
    <!--编写Cat实体类的多个Bean对象信息-->
    <bean id="cat11" class="com.kuang.pojo.Cat"></bean>
    <bean id="cat111" class="com.kuang.pojo.Cat"></bean>
    <!--编写Dog实体类的多个Bean对象信息-->
    <bean id="dog22" class="com.kuang.pojo.Dog"></bean>
    <bean id="dog222" class="com.kuang.pojo.Dog"></bean>
    <bean id="people" class="com.kuang.pojo.People"></bean>
</beans>
3.编写MyTest测试类
public class MyTest {
    @Test
    public void test2(){
        //获取上下文信息,得到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
        //从IOC容器中获取Bean信息:声明类型后就不需要强制转换了
        People people = context.getBean("people", People.class);
        //主人让宠物狗叫
        people.getDog().shout();
        //主人让宠物猫叫
        people.getCat().shout();
    }
}
4.测试结果

在这里插入图片描述

结果成功输出了猫叫和狗叫,“汪汪汪”和“喵呜“!

5.@Qualifier注解的使用总结

如果@Autowired自动装配的环境中比较复杂,自动装配无法通过一个注解【@Autowired】完成的时候,我们可以使用@Qualifier(value = “xxx”)去配置@Autowired的使用,指定一个唯一的bean对象注入!

8.4.5 @Resource注解的使用

1.修改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
           https://www.springframework.org/schema/context/spring-context.xsd">
    <!--开启注解的支持-->
    <context:annotation-config/>
     <!--编写Cat实体类的多个Bean对象信息-->
    <bean id="cat11" class="com.kuang.pojo.Cat"></bean>
    <bean id="cat111" class="com.kuang.pojo.Cat"></bean>
     <!--编写Dog实体类的多个Bean对象信息-->
    <bean id="dog22" class="com.kuang.pojo.Dog"></bean>
    <bean id="dog222" class="com.kuang.pojo.Dog"></bean>
    <bean id="people" class="com.kuang.pojo.People"></bean>
</beans>
2.修改实体类People
public class People {
    //在@Resource注解的name属性中指明唯一bean对象cat11
    @Resource(name="cat11")
    private Cat cat;
     //在@Resource注解的name属性中指明唯一bean对象dog222
    @Resource(name="dog222")
    private  Dog dog;
    private String name;
    public People(@Nullable String name) {
        this.name = name;
    }
    public Cat getCat() {
        return cat;
    }
    public Dog getDog() {
        return dog;
    }
    public String getName() {
        return name;
    }
    @Override
    public String toString() {
        return "People{" +
                "cat=" + cat +
                ", dog=" + dog +
                ", name='" + name + '\'' +
                '}';
    }
}
3.编写MyTest测试类
public class MyTest {
    @Test
    public void test2(){
        //获取上下文信息,得到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("beans3.xml");
        //从IOC容器中获取Bean信息:声明类型后就不需要强制转换了
        People people = context.getBean("people", People.class);
        //主人让宠物狗叫
        people.getDog().shout();
        //主人让宠物猫叫
        people.getCat().shout();
    }
}
4.测试结果

在这里插入图片描述

结果成功输出了猫叫和狗叫,“汪汪汪”和“喵呜“!

8.4.6 @Resource和@Autowired的区别

  • 都是通过自动装配的,都可以放在属性字段上

  • @Autowired 通过byType的方式实现,而且必须要求这个对象存在【常用】

  • @Resource 默认通过byName的方式实现,如果找不到名字,则通过byType实现,如果两个都找不到的情况下就报错【常用】

  • 执行顺序不同@Autowired默认通过byType的方式实现,@Resource默认通过byName的方式实现

8.5 作用域

8.5.1 使用单例模式

1.使用配置文件实现
1-1 修改User实体类
package com.kuang.pojo;

public class User {
    public  String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
1-2 修改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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
    <!--指定要扫描的包,这个包下的注解就会生效   -->
    <context:component-scan base-package="com.kuang"/>
    <!-- scope="singleton":单例模式
        将每个Spring IOC容器的单个bean定义范围限制为单个对象实例 -->
    <bean id="user" class="com.kuang.pojo.User" scope="singleton">
        <property name="name" value="戴沐白"></property>
    </bean>
</beans>
1-3 编写MyTest测试类
public class MyTest {
    public static void main(String[] args) {
        // 获取ApplicationContext:拿到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过IOC容器获取bean信息
        User user = (User) context.getBean("user");
        // 打印用户名字信息
        System.out.println(user.getName());
    }
}
1-4 测试结果

在这里插入图片描述

2.使用注解实现
1-1 修改User实体类
package com.kuang.pojo;

public class User {
// 使用@Scope注解, 设置作用域为单例模式
@Scope("singleton") 
// 使用@Component注解, 表示将该类作为组件注入到Spring容器中    
@Component 
public class User {
    private String name;
    @Value("宁荣荣")
    public void setName(String name) {
        this.name = name;
    }
}
1-2 修改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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
    <!-- 指定要扫描的包,这个包下的注解就会生效 -->
    <context:component-scan base-package="com.kuang"/>
</beans>
1-3 编写MyTest测试类
public class MyTest {
    public static void main(String[] args) {
        // 获取ApplicationContext:拿到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过IOC容器获取bean信息
        User user = (User) context.getBean("user");
        // 打印用户信息
        System.out.println(user);
    }
}
1-4 测试结果

在这里插入图片描述

8.5.2 使用原型模式

1.使用配置文件实现
1-1 修改User实体类
package com.kuang.pojo;

public class User {
    public  String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
1-2 修改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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
    <!-- 指定要扫描的包,这个包下的注解就会生效 -->
    <context:component-scan base-package="com.kuang"/>
    <!-- scope="prototype":原型模式
         将单个bea定义的作用域限定为任意数量的对象实例 -->
    <bean id="user" class="com.kuang.pojo.User" scope="prototype">
        <property name="name" value="宁荣荣"/>
    </bean>

</beans>
1-3 编写MyTest测试类
public class MyTest {
    public static void main(String[] args) {
        // 获取ApplicationContext:拿到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过IOC容器获取bean信息
        User user = (User) context.getBean("user");
        // 打印用户名字信息
        System.out.println(user.getName());
    }
}
1-4 测试结果

在这里插入图片描述

2.使用注解实现
1-1 修改User实体类
package com.kuang.pojo;

public class User {
// 使用@Scope注解, 设置作用域为原型模式    
@Scope("prototype") 
// 使用@Component注解, 表示将该类作为组件注入到Spring容器中 
@Component 
public class User {
    private String name;
    @Value("宁荣荣")
    public void setName(String name) {
        this.name = name;
    }
}
1-2 修改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
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 开启注解的支持 -->
    <context:annotation-config/>
    <!-- 指定要扫描的包,这个包下的注解就会生效 -->
    <context:component-scan base-package="com.kuang"/>
</beans>
1-3 编写MyTest测试类
public class MyTest {
    public static void main(String[] args) {
        // 获取ApplicationContext:拿到Spring的IOC容器
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        // 通过IOC容器获取bean信息
        User user = (User) context.getBean("user");
        // 打印用户信息
        System.out.println(user);
    }
}
1-4 测试结果

在这里插入图片描述

8.6 小结

8.6.1 xml与注解

  • xml更加万能,适用于任何场合,维护简单方便
  • 注解 不是自己的类使用不了,维护相对复杂

8.6.2 xml与注解最佳实践

  • xml用来管理bean
  • 注解只负责完成属性的注入
  • 我们在使用的过程中,只需要注意一个问题:必须让注解生效,就需要开启注解的支持
 <!--开启注解的支持-->
    <context:annotation-config/>
    <!-- 指定要扫描的包,这个包下的注解就会生效 -->
    <context:component-scan base-package="com.kuang"/>

好了,今天的有关Spring基础学习之使用注解开发的学习就到此结束啦,欢迎小伙伴们积极学习和讨论,喜欢的可以给蜗牛君点个关注,顺便来个一键三连,我们下期见,拜拜啦!


参考视频链接:https://www.bilibili.com/video/BV1WE411d7Dv(【狂神说Java】Spring5最新教程IDEA版通俗易懂)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂奔の蜗牛rz

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值