学习一个例子。测试配置bean,配置两个family,family1和family2都有相同的属性但是属性值不同,加入配置文件,可以配置如下:
<!-- 使用class属性指定类的默认构造方法创建实例 名称由id属性指定 加上scope="prototype" 表示每次生成新的实例-->
<bean id="family" class="com.study.spring.Family" >
<property name="mother" value="Lily"></property>
<property name="father" value="Bob"></property>
<property name="sister" value="Jerry"></property>
<property name="brother" value="Ham"></property>
</bean>
<bean id="familyUtil1" class="com.study.spring.FamilyUtil">
<property name="family" ref="family"></property>
</bean>
<bean id="family2" class="com.study.spring.Family" >
<property name="mother" value="family2-Lily"></property>
<property name="father" value="family2-Bob"></property>
<property name="sister" value="family2-Jerry"></property>
<property name="brother" value="family2-Ham"></property>
</bean>
<bean id="familyUtil2" class="com.study.spring.FamilyUtil">
<property name="family" ref="family2"></property>
</bean>
package com.study.spring;
public class Family{
private String mother;
private String father;
private String sister;
private String brother;
public String getMother() {
return mother;
}
public void setMother(String mother) {
this.mother = mother;
}
public String getFather() {
return father;
}
public void setFather(String father) {
this.father = father;
}
public String getSister() {
return sister;
}
public void setSister(String sister) {
this.sister = sister;
}
public String getBrother() {
return brother;
}
public void setBrother(String brother) {
this.brother = brother;
}
}
package com.study.spring;
public class FamilyUtil {
private Family family = new Family();
public Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}
public String listFamily() {
if(family.getMother() != null){
System.out.println(family.getMother());
System.out.println(family.getFather());
System.out.println(family.getBrother());
System.out.println(family.getSister());
return "success";
}else{
System.out.println("为空");
return "fail";
}
}
}
package com.study.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.study.utils.struts.BaseAction;
@Controller("testFamily")
@Scope("prototype")
public class TestFamily extends BaseAction{
/** * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么) */
private static final long serialVersionUID = 1L;
@Autowired
@Qualifier("familyUtil1")
private FamilyUtil familyUtil1;
@Autowired
@Qualifier("familyUtil2")
private FamilyUtil familyUtil2;
public void listFamily(){
if("success".equals(familyUtil1.listFamily())){
writeJson2Page("success");
}else{
writeJson2Page("fail");
}
}
public void listFamily2(){
if("success".equals(familyUtil2.listFamily())){
writeJson2Page("success");
}else{
writeJson2Page("fail");
}
}
}
调用TestFamily.listFamily()时,控制台打印结果
Lily
Bob
Ham
Jerry
调用TestFamily.ListFamily2()时,控制台打印结果
family2-Lily
family2-Bob
family2-Ham
family2-Jerry
结果与预期的一样。
TestFamily通过注解的方式加载bean “familyUtil1”,在spring-base.xml配置文件中
即,familyUtil1有family属性,且指向bean “family”,而bean “family”又有以下属性
<property name="mother" value="Lily"></property>
<property name="father" value="Bob"></property>
<property name="sister" value="Jerry"></property>
<property name="brother" value="Ham"></property>
故调用familyUtil1.listFamily()时会显示配置文件的family属性配置。调用familyUtil2.listFamily同理会显示family2的属性配置。
CXF implements the JAX-WS APIs which make building web services easy.
即是apache的一个webservice引擎。
spring4依赖的包
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
cxf3依赖的jar包
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.0-milestone2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.0-milestone1</version>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>gyh</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*.xml</param-value>
</context-param>
<!-- CXF implements the JAX-WS APIs which make building web services easy.apache的一个webservice引擎。 -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<bean id="helloService" class="com.study.spring.HelloServiceImpl"></bean>
<jaxrs:server id="defaultService" address="/">
<jaxrs:serviceBeans>
<ref bean="helloService"/>
</jaxrs:serviceBeans>
</jaxrs:server>
接口类IHelloService
package com.study.spring;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import com.study.utils.spring.IBaseAction;
@Path("/hello")
public interface IHelloService extends IBaseAction{
@GET
@Path("/sayHello")
@Produces({MediaType.TEXT_HTML,MediaType.APPLICATION_XML,MediaType.APPLICATION_JSON})
public String sayHello(@FormParam("msg")String msg);
}
接口实现类HelloServiceImpl
package com.study.spring;
public class HelloServiceImpl implements IHelloService{
@Override
public String sayHello(String msg) {
System.out.println("Hello World!" + msg);
return "success";
}
}
启动tomcat,访问地址由tomcat的地址+项目名称(tomcat下的server.xml可以修改)+web.xml中cxfServlet配置的 地址+spring-cxf.xml中配置的address+interface配置path
如我的ip地址 192.168.100.115:8081,项目名gyh,
web.xml配置 “/service”,
spring-cxf.xml配置 “/”
类Path: “/hello”
方法Path: “/sayHello”
则访问地址 :http://192.168.100.115:8081/gyh/service/hello/sayHello
加参数访问:http://192.168.100.115:8081/gyh/service/hello/sayHello?msg=Sweet
浏览器结果
控制台输出,上面为加参数访问结果