个人随笔
目录
环境搭建:maven版Spring5开发环境搭建(配置文件模式、纯注解模式)
2020-09-11 22:53:36

下面搭建一下纯净版的spring5环境,主要实现的功能是配置文件和注解方式获取bean,代码例子如下:

一、依赖pom.xml中

  1. <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. <version>5.2.8.RELEASE</version>
  6. </dependency>

二、带配置文件版

1、配置文件applicationContext.xml

这里需要支持注解模式

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context.xsd">
  9. <!--编写对应的配置信息-->
  10. <bean class="com.suibibk.spring.entity.User" id="user"/>
  11. <!-- 开启注解 -->
  12. <context:component-scan base-package="com.suibibk.spring"/>
  13. </beans>

注意:若要支持注解模式,头文件里需要有如下内容:

  1. xmlns:context="http://www.springframework.org/schema/context"
  2. http://www.springframework.org/schema/context
  3. http://www.springframework.org/schema/context/spring-context.xsd

2、bean对象Car和User

User是用配置文件的方式初始化bean,Car是用注解的方式初始化bean。

  1. package com.suibibk.spring.entity;
  2. public class User {
  3. private String username;
  4. private int age;
  5. public String getUsername() {
  6. return username;
  7. }
  8. public void setUsername(String username) {
  9. this.username = username;
  10. }
  11. public int getAge() {
  12. return age;
  13. }
  14. public void setAge(int age) {
  15. this.age = age;
  16. }
  17. @Override
  18. public String toString() {
  19. return "User [username=" + username + ", age=" + age + "]";
  20. }
  21. }
  1. package com.suibibk.spring.entity;
  2. import org.springframework.stereotype.Component;
  3. @Component
  4. public class Car {
  5. private String carName;
  6. public String getCarName() {
  7. return carName;
  8. }
  9. public void setCarName(String carName) {
  10. this.carName = carName;
  11. }
  12. @Override
  13. public String toString() {
  14. return "Car [carName=" + carName + "]";
  15. }
  16. }

可以看到,Car上有注解@Component

3、测试类

  1. public class App {
  2. @SuppressWarnings("resource")
  3. public static void main(String[] args) {
  4. //获取上下文对象ApplicationContext
  5. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
  6. //获取实例,配置文件
  7. User user = (User)context.getBean("user");
  8. user.setUsername("suibibk.com");
  9. user.setAge(18);
  10. System.out.println(user);
  11. //获取实例,注解
  12. Car car = (Car)context.getBean("car");
  13. car.setCarName("宝马");
  14. System.out.println(car);
  15. }
  16. }

4、结果

  1. User [username=suibibk.com, age=18]
  2. Car [carName=宝马]

三、纯注解版

现在一般都不用配置文件的方式,直接注解即可。

1、配置文件类

  1. @Configuration
  2. @ComponentScan(basePackages = "com.suibibk.spring")
  3. public class SpringConfiguration {
  4. //spring容器初始化时,会调用配置类的无参构造函数
  5. public SpringConfiguration(){
  6. System.out.println("容器启动初始化");
  7. }
  8. }

2、User

  1. @Component
  2. public class User {
  3. private String userName;
  4. public String getUserName() {
  5. return userName;
  6. }
  7. public void setUserName(String userName) {
  8. this.userName = userName;
  9. }
  10. @Override
  11. public String toString() {
  12. return "User [userName=" + userName + "]";
  13. }
  14. }

User需要在配置文件类的扫描包下面

3、测试类

  1. public class App {
  2. public static void main(String[] args) {
  3. ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
  4. User user = (User) context.getBean("user");
  5. user.setUserName("个人随笔");
  6. System.out.println(user);
  7. }
  8. }

4、运行结果

  1. 容器启动初始化
  2. User [userName=个人随笔]

完成。

 568

啊!这个可能是世界上最丑的留言输入框功能~


当然,也是最丑的留言列表

有疑问发邮件到 : suibibk@qq.com 侵权立删
Copyright : 个人随笔   备案号 : 粤ICP备18099399号-2