关于spring,以及spring基于XML文件和注解的简单实现

一时失言乱红尘 2022-05-14 05:43 93阅读 0赞

什么是spring?

Spring是一个开源的轻量级Java SE(Java 标准版本)/Java EE(Java 企业版本)开发应用框架,其目的是用于简化企业级应用程序开发。是一个开源框架,是一个bean类工厂,核心是控制反转(IOC)、依赖注入(DI)和面向切面(AOP)。
在日常开发过程中,能提高我们的开发速度以及程序的性能:

  1. 高内聚低耦合:
    Spring就是一个大工厂(容器),可以将所有对象创建和依赖关系维护,交给Spring管理 ,客户端需要某个对象从而不需要主动的调用,而是对象被动的直接注入(DI)过来;
  2. AOP编程的支持
    Spring提供面向切面编程,可以方便的实现对程序进行权限拦截、运行监控等功能
  3. 方便集成各种优秀框架
    Spring不排斥各种优秀的开源框架,其内部提供了对各种优秀框架(如:Struts、Hibernate、MyBatis、Quartz等)的直接支持
  4. 降低JavaEE API的使用难度
    Spring 对JavaEE开发中非常难用的一些API(JDBC、JavaMail、远程调用等),都提供了封装,使这些API应用难度大大降低。

案例:控制反转、依赖注入bean类基于XML文件

1.pom文件配置
根据创建的Maven项目,项目所需要依赖何种jar包都可以直接去maven仓库找到依赖源。(maven仓库:https://mvnrepository.com/)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.yc.hui</groupId>
  5. <artifactId>maven_spring</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <name>maven_spring Maven Webapp</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://www.example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.7</maven.compiler.source>
  14. <maven.compiler.target>1.7</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.11</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <!--下面这俩个为spring的依赖jar包,必须要有这俩个,我们的spring容器才能加载项目才能启动成功-->
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-context</artifactId>
  27. <version>5.0.6.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-core</artifactId>
  32. <version>5.0.6.RELEASE</version>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <finalName>maven_spring</finalName>
  37. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  38. <plugins>
  39. <plugin>
  40. <artifactId>maven-clean-plugin</artifactId>
  41. <version>3.0.0</version>
  42. </plugin>
  43. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  44. <plugin>
  45. <artifactId>maven-resources-plugin</artifactId>
  46. <version>3.0.2</version>
  47. </plugin>
  48. <plugin>
  49. <artifactId>maven-compiler-plugin</artifactId>
  50. <version>3.7.0</version>
  51. </plugin>
  52. <plugin>
  53. <artifactId>maven-surefire-plugin</artifactId>
  54. <version>2.20.1</version>
  55. </plugin>
  56. <plugin>
  57. <artifactId>maven-war-plugin</artifactId>
  58. <version>3.2.0</version>
  59. </plugin>
  60. <plugin>
  61. <artifactId>maven-install-plugin</artifactId>
  62. <version>2.5.2</version>
  63. </plugin>
  64. <plugin>
  65. <artifactId>maven-deploy-plugin</artifactId>
  66. <version>2.8.2</version>
  67. </plugin>
  68. </plugins>
  69. </pluginManagement>
  70. </build>
  71. </project>

其中….依赖jar包
2.在src/main/resource路径下创建一个xxxx.xml作为spring的容器配置文件
我这里命名为application.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <bean id="student" class="com.yc.bean.Student">
  4. <property name="sid" value="1"></property>
  5. <property name="sname" value="hui"></property>
  6. </bean>
  7. </beans>

3.bean类
封装一个和application.xml配置文件中的类

  1. package com.yc.bean;
  2. import java.io.Serializable;
  3. public class Student implements Serializable{
  4. private int sid;
  5. private String sname;
  6. @Override
  7. public String toString() {
  8. return "Student [sid=" + sid + ", sname=" + sname + "]";
  9. }
  10. /** * */
  11. private static final long serialVersionUID = 1L;
  12. public Student() {
  13. super();
  14. // TODO Auto-generated constructor stub
  15. }
  16. public int getSid() {
  17. return sid;
  18. }
  19. public void setSid(int sid) {
  20. this.sid = sid;
  21. }
  22. public String getSname() {
  23. return sname;
  24. }
  25. public void setSname(String sname) {
  26. this.sname = sname;
  27. }
  28. public Student(int sid, String sname) {
  29. super();
  30. this.sid = sid;
  31. this.sname = sname;
  32. }
  33. }

4.测试代码

  1. package com.yc.test;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. import com.yc.bean.Student;
  6. public class TTest {
  7. @Test
  8. public void test1(){
  9. ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {
  10. "application.xml"});
  11. Student s=(Student) context.getBean("student");
  12. System.out.println(s);
  13. }

5.测试结果:

这里写图片描述

案例:控制反转、依赖注入bean类基于注解

1.pom文件配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.yc.hui</groupId>
  5. <artifactId>maven_spring</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>war</packaging>
  8. <name>maven_spring Maven Webapp</name>
  9. <!-- FIXME change it to the project's website -->
  10. <url>http://www.example.com</url>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.source>1.7</maven.compiler.source>
  14. <maven.compiler.target>1.7</maven.compiler.target>
  15. </properties>
  16. <dependencies>
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.11</version>
  21. <scope>test</scope>
  22. </dependency>
  23. <!--下面这俩个为spring的依赖jar包,必须要有这俩个,我们的spring容器才能加载项目才能启动成功-->
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-context</artifactId>
  27. <version>5.0.6.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-core</artifactId>
  32. <version>5.0.6.RELEASE</version>
  33. </dependency>
  34. </dependencies>
  35. <build>
  36. <finalName>maven_spring</finalName>
  37. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  38. <plugins>
  39. <plugin>
  40. <artifactId>maven-clean-plugin</artifactId>
  41. <version>3.0.0</version>
  42. </plugin>
  43. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  44. <plugin>
  45. <artifactId>maven-resources-plugin</artifactId>
  46. <version>3.0.2</version>
  47. </plugin>
  48. <plugin>
  49. <artifactId>maven-compiler-plugin</artifactId>
  50. <version>3.7.0</version>
  51. </plugin>
  52. <plugin>
  53. <artifactId>maven-surefire-plugin</artifactId>
  54. <version>2.20.1</version>
  55. </plugin>
  56. <plugin>
  57. <artifactId>maven-war-plugin</artifactId>
  58. <version>3.2.0</version>
  59. </plugin>
  60. <plugin>
  61. <artifactId>maven-install-plugin</artifactId>
  62. <version>2.5.2</version>
  63. </plugin>
  64. <plugin>
  65. <artifactId>maven-deploy-plugin</artifactId>
  66. <version>2.8.2</version>
  67. </plugin>
  68. </plugins>
  69. </pluginManagement>
  70. </build>
  71. </project>

其中….依赖jar包
2.创建一个类作为spring的容器

  1. package com.yc.bean;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.ComponentScan;
  4. import org.springframework.context.annotation.Configuration;
  5. @Configuration //表示这是个容器
  6. public class App {
  7. @Bean
  8. public Student student(){
  9. Student s= new Student();
  10. s.setSid(1);
  11. s.setSname("a");
  12. return s;
  13. }
  14. }

3.bean类

  1. package com.yc.bean;
  2. import java.io.Serializable;
  3. import org.springframework.stereotype.Component;
  4. import org.springframework.stereotype.Repository;
  5. //@Repository //Dao层
  6. //@Component //通用注解
  7. public class Student implements Serializable{
  8. private int sid;
  9. private String sname;
  10. @Override
  11. public String toString() {
  12. return "Student [sid=" + sid + ", sname=" + sname + "]";
  13. }
  14. /** * */
  15. private static final long serialVersionUID = 1L;
  16. public Student() {
  17. super();
  18. // TODO Auto-generated constructor stub
  19. }
  20. public int getSid() {
  21. return sid;
  22. }
  23. public void setSid(int sid) {
  24. this.sid = sid;
  25. }
  26. public String getSname() {
  27. return sname;
  28. }
  29. public void setSname(String sname) {
  30. this.sname = sname;
  31. }
  32. public Student(int sid, String sname) {
  33. super();
  34. this.sid = sid;
  35. this.sname = sname;
  36. }
  37. }

4.测试代码

  1. package com.yc.test;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.annotation.AnnotationConfigApplicationContext;
  5. import com.yc.bean.App;
  6. import com.yc.bean.Student;
  7. public class TTest {
  8. @Test
  9. public void test5(){
  10. ApplicationContext context=new AnnotationConfigApplicationContext(App.class);
  11. Student s=(Student)context.getBean("student");
  12. System.out.println(s);
  13. }

5.测试结果
这里写图片描述


下一篇咱们介绍springmvc的相关知识,编写不易,谢谢各位支持。

发表评论

表情:
评论列表 (有 0 条评论,93人围观)

还没有评论,来说两句吧...

相关阅读