您现在的位置是:首页 > 个人日记个人日记

spring-01-快速上手

2019-02-02 19:24:47【个人日记】306人已围观

简介spring是一个开源框架,以强大的IOC和AOP的以闻名,如今我也深爱,投入其中,不愧是程序员的春天

快速上手

配置pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.spring</groupId>
  7. <artifactId>spring</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <dependencies>
  10. <!--spring框架导入-->
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-context</artifactId>
  14. <version>4.3.17.RELEASE</version>
  15. </dependency>
  16. <!-- 单元测试 -->
  17. <dependency>
  18. <groupId>junit</groupId>
  19. <artifactId>junit</artifactId>
  20. <version>4.12</version>
  21. <scope>test</scope>
  22. </dependency>
  23. </dependencies>
  24. </project>

配置完成则导入spring依赖的jar包

例如有一个Bean对象
配置这个Message.java文件

  1. package com.spring;
  2. public class MessageService {
  3. private String message;
  4. MessageService(){
  5. System.out.println("test");
  6. }
  7. public String getMessage() {
  8. return message;
  9. }
  10. public void setMessage(String message) {
  11. this.message = message;
  12. }
  13. }

spring-config.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. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
  5. <!--id:用于从spring容器获得实例时使用-->
  6. <!--class:需要创建实例化的全限定名-->
  7. <!--property:类中的成员-->
  8. <!--scope是bean的作用域-->
  9. <!--scope属性有singleton单例模式,prototype多例模式-->
  10. <!--默认是单例模式-->
  11. <bean id="messageService" class="com.spring.MessageService" scope="singleton">
  12. <!--name:成员的名称-->
  13. <!--value:对应属性的值-->
  14. <property name="message" value="hello spring"/>
  15. </bean>
  16. </beans>

Junit

配置test.java文件

  1. package com.spring;
  2. import org.junit.Test;
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5. public class MessageServiceTest {
  6. @Test
  7. public void setMessage() {
  8. // 读取全局配置文件
  9. ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
  10. "spring-config.xml"
  11. );
  12. // 获取Java对象
  13. MessageService messageService = (MessageService) applicationContext.getBean("messageService");
  14. MessageService messageService1 = applicationContext.getBean(MessageService.class);
  15. String message = messageService.getMessage();
  16. System.out.println(message);
  17. }
  18. }

控制台输出为

  1. D:\Java\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 -javaagent:D:\IntelliJ\lib\idea_rt.jar=4895:D:\IntelliJ\bin -Dfile.encoding=UTF-8 -classpath D:\IntelliJ\lib\idea_rt.jar;D:\IntelliJ\plugins\junit\lib\junit-rt.jar;D:\IntelliJ\plugins\junit\lib\junit5-rt.jar;D:\ideaProjects\spring\target\test-classes;D:\ideaProjects\spring\target\classes;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\4.3.17.RELEASE\spring-context-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\4.3.17.RELEASE\spring-aop-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\4.3.17.RELEASE\spring-beans-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\4.3.17.RELEASE\spring-core-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\4.3.17.RELEASE\spring-expression-4.3.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 com.spring.MessageServiceTest,setMessage
  2. 1 06, 2019 1:44:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
  3. 信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2890c451: startup date [Sun Jan 06 13:44:16 CST 2019]; root of context hierarchy
  4. 1 06, 2019 1:44:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
  5. 信息: Loading XML bean definitions from class path resource [spring-config.xml]
  6. test
  7. hello spring
  8. Process finished with exit code 0

这里默认使用单例模式,可根据实际情况去挑选多例

Tags: 网站  

评论区

    2019-09-18 23:52:31

    站长

    欢迎各位评论!


文章评论



给自个选个头像吧!






站点信息

  • 建站时间:   2019-01-31
  • 网站程序:   Tomcat+nginx
  • 文章统计:   44篇文章
  • 标签管理:   标签云
  • 微信公众号:  扫描二维码,联系我