您现在的位置是:首页 > 个人日记个人日记
spring-01-快速上手
2019-02-02 19:24:47【个人日记】849人已围观
简介spring是一个开源框架,以强大的IOC和AOP的以闻名,如今我也深爱,投入其中,不愧是程序员的春天
快速上手
配置pom.xml
文件
<?xml version="1.0" encoding="UTF-8"?>
<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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.spring</groupId>
<artifactId>spring</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--spring框架导入-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
<!-- 单元测试 -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
配置完成则导入spring依赖的jar包
例如有一个Bean对象
配置这个Message.java
文件
package com.spring;
public class MessageService {
private String message;
MessageService(){
System.out.println("test");
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
在spring-config.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--id:用于从spring容器获得实例时使用-->
<!--class:需要创建实例化的全限定名-->
<!--property:类中的成员-->
<!--scope是bean的作用域-->
<!--scope属性有singleton单例模式,prototype多例模式-->
<!--默认是单例模式-->
<bean id="messageService" class="com.spring.MessageService" scope="singleton">
<!--name:成员的名称-->
<!--value:对应属性的值-->
<property name="message" value="hello spring"/>
</bean>
</beans>
Junit
配置test.java
文件
package com.spring;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MessageServiceTest {
@Test
public void setMessage() {
// 读取全局配置文件
ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
"spring-config.xml"
);
// 获取Java对象
MessageService messageService = (MessageService) applicationContext.getBean("messageService");
MessageService messageService1 = applicationContext.getBean(MessageService.class);
String message = messageService.getMessage();
System.out.println(message);
}
}
控制台输出为
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
1月 06, 2019 1:44:16 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2890c451: startup date [Sun Jan 06 13:44:16 CST 2019]; root of context hierarchy
1月 06, 2019 1:44:16 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [spring-config.xml]
test
hello spring
Process finished with exit code 0
这里默认使用单例模式,可根据实际情况去挑选多例
Tags: 网站
下一篇: mybatis-06-映射关系
相关文章
随机图文
评论区
2024-12-21 19:10:42
站长
没有登录功能是为了方便大家留言,但留言接口现在被恶意攻击,将关闭留言接口,如有疑问,请联系我的QQ 1538933906/同微信
2020-01-13 16:22:00
兄弟网站做的太棒了