【Spring】第一章 简单示例
2015-06-26 15:24
351 查看
一、spring简单demo
1.引入jar文件
springframework-core.jar
springframework-beans.jar
springframework-context.jar
springframework-context-support.jar
2.编写配置文件:*.properties
3. 编写spring配置文件
4. 如果要用properties中的值
需要在spring-test.xml中引入properties文件:PropertiesPlaceholderConfigurer。在其它bean中通过${key}来使用
要在具体类中使用,需要将具体类的全局变量,并添加getter(),setter()方法
5. 运行代码时需要加载spring.xml,并通过spring上下文创建bean。
二、使用注解方式启动
1. 在配置文件中添加以下代码:使用注解,import是引入其它spring配置文件
2. 在类中引入代码时可以通过
<context:annotation-config /> <context:component-scan base-package="util" />
1.引入jar文件
springframework-core.jar
springframework-beans.jar
springframework-context.jar
springframework-context-support.jar
2.编写配置文件:*.properties
login.username=zy login.password=123456
3. 编写spring配置文件
<beans ...> <bean id="propertyConfiger" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:login.properties"/> </bean> <bean id="loginService" class=" com.jd.mtroom.mtroom_core.service.impl.LoginServiceImpl"> <property name="username" value="${login.username}"/> <property name="password" value="${login.password}"/><pre name="code" class="java"></bean></bean>
4. 如果要用properties中的值
需要在spring-test.xml中引入properties文件:PropertiesPlaceholderConfigurer。在其它bean中通过${key}来使用
要在具体类中使用,需要将具体类的全局变量,并添加getter(),setter()方法
package com.jd.mtroom.mtroom_core.bean; public class Bean { private String username; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
5. 运行代码时需要加载spring.xml,并通过spring上下文创建bean。
public class Test { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring-login.xml"); LoginService loginService =(LoginService) context.getBean("loginService"); loginService.login(); } }
二、使用注解方式启动
1. 在配置文件中添加以下代码:使用注解,import是引入其它spring配置文件
<context:annotation-config /> <context:component-scan base-package="util" />
<import resource="classpath*:/spring/spring-jdbc.xml" />
2. 在类中引入代码时可以通过
@ContextConfiguration(locations = "classpath*:/spring/spring-test.xml") public class BaseTest extends AbstractTestNGSpringContextTests { protected static XmlTranslator xmlTool = new XmlTranslator(); @Resource protected DataSource customerDataSource; @Value("#{db['customer.name']}") public String customerUserName ; }
相关文章推荐
- {嵌入式学习笔记(一)}ubuntu下嵌入式开发环境搭建
- java中的sleep()和wait()的区别
- JAVA多线程编程(三)——线程的生命周期(sleep,join,interrupt)
- JVM学习笔记(二)------Java代码编译和执行的整个过程
- JVM学习笔记(二)------Java代码编译和执行的整个过程
- Spring整合RMI
- 错误笔记:Caused by: java.lang.StackOverflowError
- java类的加载以及初始化顺序
- Spring MVC使用拦截器实现权限控制
- QA:java.lang.RuntimeException:java.io.FileNotFoundException:Resource nexus-maven-repository-index.properties does not exist.
- R语言RJava安装步骤
- Spring学习(3)---Spring设值注入和构造注入
- Eclipse中出现无法找到Maven包
- Java学习-016-CSV 文件读取实例源代码
- 在eclipse中搭建SSH框架
- VisualRules 调用Java类
- java学习之旅22--综合练习_九九乘法表
- Java内部类详解
- Hibernate4 与 spring3 集成之后 “No Session found for current thread”
- 如何创建Java中的线程池