Java EE --- Spring 的创建和使用

x33g5p2x  于2022-05-28 转载在 Java  
字(4.3k)|赞(0)|评价(0)|浏览(660)

1. Spring 项目的创建

1.1 创建一个 Maven 项目

配置一下国内源

settings.xml 内容

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  4. <!-- 配置国内源 -->
  5. <!-- 重点就是这个 mirros,如果存在settings.xml 只拷贝这一段即可 -->
  6. <mirrors>
  7. <mirror>
  8. <id>alimaven</id>
  9. <name>aliyun maven</name>
  10. <url>http://maven.aliyun.com/nexus/content/groups/public/</url>
  11. <mirrorOf>central</mirrorOf>
  12. </mirror>
  13. </mirrors>
  14. <profiles>
  15. <profile>
  16. <id>jdk-1.8</id>
  17. <activation>
  18. <activeByDefault>true</activeByDefault>
  19. <jdk>1.8</jdk>
  20. </activation>
  21. <properties>
  22. <maven.compiler.source>1.8</maven.compiler.source>
  23. <maven.compiler.target>1.8</maven.compiler.target>
  24. <maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
  25. </properties>
  26. </profile>
  27. <profile>
  28. <id>nexus</id>
  29. <repositories>
  30. <repository>
  31. <id>central</id>
  32. <url>http://repo.maven.apache.org/maven2</url>
  33. <snapshots>
  34. <enabled>false</enabled>
  35. </snapshots>
  36. </repository>
  37. <repository>
  38. <id>ansj-repo</id>
  39. <name>ansj Repository</name>
  40. <url>http://maven.nlpcn.org/</url>
  41. <snapshots>
  42. <enabled>false</enabled>
  43. </snapshots>
  44. </repository>
  45. </repositories>
  46. </profile>
  47. </profiles>
  48. <activeProfiles>
  49. <activeProfile>nexus</activeProfile>
  50. </activeProfiles>
  51. </settings>

这里的注意事项:

  1. 首先勾选这两个 选项
  2. 检查 settings.xml 这个目录是否存在
    如果不存在, 就要把这个settings.xml创建进去
    如果存在, 就要查看自己是否配置了国内源
  3. 如果还是不行, 就把repository目录下的内容都删了

1.2 添加 Spring 依赖包

  1. <dependencies>
  2. <dependency>
  3. <groupId>org.springframework</groupId>
  4. <artifactId>spring-context</artifactId>
  5. <version>5.2.3.RELEASE</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>org.springframework</groupId>
  9. <artifactId>spring-beans</artifactId>
  10. <version>5.2.3.RELEASE</version>
  11. </dependency>
  12. </dependencies>

添加之后 记得刷新一下maven

1.3 创建 启动类

2. 将 Bean 对象存储到 Spring

2.1 创建一个业务对象

2.2 将对象存储到Spring框架 (声明式)

将下列内容拷贝进去

  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6. </beans>

将 User 对象注册到 Spring 中.

  1. <beans>
  2. <bean id="user" class="model.UserBean"></bean>
  3. </beans>

3. 获取并使用 Bean 对象

3.1 获取 Spring 上下文对象

方式一: 通过 ApplicationContext 来获取

  1. // 方法一: 通过 ApplicationContext 来获取 Spring 的上下文对象
  2. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");

方法二: 通过 BeanFactory 来获取

  1. // 方法二: 通过 BeanFactory 来获取 Spring 的上下文对象
  2. BeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("spring-config.xml"));

注意事项

3.2 通过 Spring 上下文对象来获取 Bean 对象

方式一: 通过 bean 的 id 来获取

  1. // 方法一: 通过 bean 的 id 来获取
  2. UserBean user = (UserBean) context.getBean("user");

缺点 : 需要强制类型转换

方式二: 通过类型获取

  1. // 方法二: 通过类型获取
  2. UserBean user = context.getBean(UserBean.class);

缺点 : 对于多个对象的同一种类型的 Bean 获取会报错

方法三: 通过 id+类型 来获取

  1. // 方法三: 通过 id+类型 来获取
  2. UserBean user = context.getBean("user",UserBean.class);

优点 : 不需要强制类型转换, 对于多个对象的同一种类型的 Bean 获取不会报错

注意事项

3.3 使用业务对象

  1. public static void main(String[] args) {
  2. // 1. 获取到 Spring 的上下文对象
  3. // 方法一: 通过 ApplicationContext 来获取 Spring 的上下文对象
  4. ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml");
  5. // 2. 通过 Spring 上下文对象来获取业务对象
  6. // 方法一: 通过 bean 的 id 来获取
  7. UserBean user = (UserBean) context.getBean("user");
  8. // 3. 使用业务对象
  9. user.printName("Spring");
  10. }

4. ApplicationContext VS BeanFactory

1. 二者来自的 jar 包不一样

ApplicationContext 来自 spring.context.jar
BeanFactory 来自 spring.beans.jar

2. 对于继承来说

ApplicationContextBeanFactory 都属于 Spring 下的顶级接口
 
ApplicationContext 属于 BeanFactory 的⼦类, BeanFactory 所有的功能 ApplicationContext 也是拥有的,
 
除此之外. AppicationContext 还拥有其他的 BeanFactory 没有的功能.
 
例如 对国际化支持、资源访问支持、以及事件传播等方面的支持

3. 对于执行性能来说

ApplicationContext⼀次性加载并初始化所有的 Bean 对象,所以它的启动过程可能比较慢, 但是后续执行快; ⽽ BeanFactory 是需要那个才去加载那个,因此 BeanFactory 占用系统资源更少, 启动更快, 但是后续的执行可能会更慢些.

相关文章