Spring-HelloWorld搭建
来源:互联网 发布:顶级富豪 知乎 编辑:程序博客网 时间:2024/05/18 00:16
步骤:
1.首先编写JAVABEAN
2.配置CONFIG.XML
3.编写测试程序
LOG4J的配置
log4j.rootLogger=DEBUG\uFF0Cstdoutlog4j.logger.org=ERROR,A1log4j.appender.A1=org.apache.log4j.RollingFileAppenderlog4j.appender.A1.File=org.loglog4j.appender.A1.MaxFileSize=500KBlog4j.appender.A1.MaxBackupIndex=50log4j.appender.A1.Append=truelog4j.appender.A1.layout=org.apache.log4j.PatternLayoutlog4j.appender.A1.layout.ConversionPattern=%d{ISO8601}-[%p][%c{1}]-%m%nlog4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.File=gf.loglog4j.appender.stdout.MaxFileSize=500KBlog4j.appender.stdout.MaxBackupIndex=50log4j.appender.stdout.Append=truelog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.CoversionPattern=[%-5p]%d{yyyy-MM-dd HH:mm:ss}%c-%m%n
编写存放HELLOWORLD的JAVA文件HELLOWORLD.JAVA
这个JAVABEAN用来存储将要展示的HELLOWORLD,并提供GET和SET方法
package com.gc.action;public class HelloWorld {public String msg=null;//该变量用来存储字符串//设定变量msg的set方法public void setMsg(String msg) {this.msg=msg;}//获取变量msg的get方法public String getMsg() {return this.msg;}}
配置SPRING的XML
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!--定义一个Bean--> <bean id="HelloWorld" class="com.gc.action.HelloWorld"> <!--将其变量msg通过依赖注入--> <property name="msg"> <value>opzoonzhuzhengke</value> </property> </bean></beans>
id="HelloWorld"用来唯一表示该BEAN class="com.gc.action.HelloWorld" name="msg"和对应变量对应的<value>opzoonzhuzhengke</value>,希望向JAVABEAN中注入字符串“opzoonzhuzhengke”测试程序:
package com.gc.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.gc.action.HelloWorld;public class TestHelloWorld { public static void main(String[] args) { //通过ApplicationContext来获取Spring文件的配置 ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml"); //通过Bean的id来获取Bean HelloWorld HelloWorld=(HelloWorld)actx.getBean("HelloWorld"); //打印输出 System.out.println(HelloWorld.getMsg()); }}
测试程序利用ApplicationContext来获取config.xml,从而完成了JAVABEAN与XML之间关系的建立,接着就可以用getBean()方法获取在XML中定义的内容了
SPRING可以通过XML定义任意字符串,IOC,面向切面。
还有修改其来源(如下):
1.首先编写接口文件hello.java
2.通过接口,重写接口方法,GET和SET方法实现,完成EnHello.java和ChHello.java
3.修改CONFIG.XML
4.测试程序
1.接口
package com.gc.impl;public interface Hello { //该方法用来输出问候语 public String doSalutation();}2.两个类
package com.gc.action;import com.gc.impl.Hello;public class ChHello implements Hello{//该变量用来存储字符串public String msg=null;//设定变量msg的set方法public void setMsg(String msg) {this.msg=msg;}//获取变量msg的get方法public String getMsg() {return this.msg;}@Overridepublic String doSalutation() {// TODO Auto-generated method stubreturn "你好"+this.msg;} }package com.gc.action;import com.gc.impl.Hello;public class EnHello implements Hello{//该变量用来存储字符串public String msg=null;//设定变量msg的set方法public void setMsg(String msg) {this.msg=msg;}//获取变量msg的get方法public String getMsg() {return this.msg;}@Overridepublic String doSalutation() {// TODO Auto-generated method stubreturn "Hello"+this.msg;} }
3.config.xml<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!--定义一个Bean--> <bean id="HelloWorld" class="com.gc.action.EnHello"> <!--将其变量msg通过依赖注入--> <property name="msg"> <value>english zhuzhengke</value> </property> </bean></beans>4.测试程序
package com.gc.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.FileSystemXmlApplicationContext;import com.gc.action.HelloWorld;import com.gc.impl.Hello;public class TestHelloWorld { public static void main(String[] args) { //通过ApplicationContext来获取Spring文件的配置 ApplicationContext actx=new FileSystemXmlApplicationContext("config.xml"); //通过Bean的id来获取Bean Hello hello=(Hello)actx.getBean("HelloWorld"); //打印输出 System.out.println(hello.doSalutation()); }}输出:
Helloenglish zhuzhengke
修改CONFIG.XML的CLASS
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd"><beans> <!--定义一个Bean--> <bean id="HelloWorld" class="com.gc.action.ChHello"> <!--将其变量msg通过依赖注入--> <property name="msg"> <value>english zhuzhengke</value> </property> </bean></beans>
运行TEST程序,输出:你好english zhuzhengke
仅仅改变了CONFIG的CLASS来源,测试程序的输出结果就改变了。
- Spring-HelloWorld搭建
- jersey+spring+maven 搭建实例helloworld
- Spring Boot之HelloWorld环境搭建!!!
- Spring boot入门一 环境搭建HelloWorld
- 快速搭建Spring boot CRUD-HelloWorld程序
- Gradle + Spring Boot 搭建一个 helloworld工程
- SSM搭建-Spring MVC搭建第一个HelloWorld(12)
- SSM搭建-Spring第一个Spring HelloWorld(2)
- spring-mvc环境搭建及helloworld的demo实现
- SPRING BOOT快速搭建WEB应用教程(二)—HELLOWORLD
- spring笔记--从零搭建一个简单的HelloWorld程序
- Java Spring框架 + maven+Beans 搭建一个HelloWorld
- java轮子-5分钟搭建spring boot web工程helloworld
- Spring-001-环境搭建与第一个HelloWorld
- Spring Boot学习笔记:(一)简介与HelloWorld搭建
- Spring helloworld
- spring helloworld
- spring helloworld
- 属性页表单和向导页
- java动态代理(JDK和cglib)
- 关于graph cut
- 程序员真的是吃青春饭的吗?(献给即将进入职场的程序员们)
- 加入jpa规范后,JavaBean的要求
- Spring-HelloWorld搭建
- oracle 关闭listener日志
- 复制excel下拉框的数值
- ASP.net 如何限制上传文件的大小
- spring AOP原理
- 次优二叉树
- poj 2612 扫雷 模拟
- C# winform自定义Label控件使其能设置行距
- 索引