一、入门示例1
1.简单配置spring环境
2.写一个普通的类
package com.gao;
public class Hello {
public void sayHello(String name)
{
System.out.println("Hello,"+name);
}
}
3.配置文件 applicationContext.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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="hello" class="com.gao.Hello"></bean>
</beans>
4.编写一个测试类
package com.gao;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
Hello h=(Hello)factory.getBean("hello");
h.sayHello("gao");
}
}
运行结果:
二、在配置文件中加入JavaBean的属性初始值(集合和数组类型)
1.编写javabean类
package com.bean;
import java.util.*;
public class Bean {
private String arr[];
private List list;
private Map map;
private Set set;
public String[] getArr() {
return arr;
}
public void setArr(String[] arr) {
this.arr = arr;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public Map getMap() {
return map;
}
public void setMap(Map map) {
this.map = map;
}
public Set getSet() {
return set;
}
public void setSet(Set set) {
this.set = set;
}
}
2.在配置文件中注入属性的初始值
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="hello" class="com.gao.Hello"></bean>
<bean id="bean" class="com.bean.Bean">
<property name="arr"><value>java,javaEE,javaSE</value></property>
<property name="list">
<list>
<value>huang</value>
<value>li</value>
<value>gao</value>
</list>
</property>
<property name="set">
<set>
<value>ground</value>
<value>god</value>
<value>human</value>
</set>
</property>
<property name="map">
<map>
<entry key="a" value="A"></entry>
<entry key="b" value="B"></entry>
<entry key="c" value="C"></entry>
</map>
</property>
</bean>
</beans>
3.编写一个测试类
package com.bean;
import java.util.*;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
Bean bean=(Bean)factory.getBean("bean");
String[] str=bean.getArr();
System.out.println("arr[]");
for(int i=0;i<str.length;i++)
{
System.out.println(str[i]);
}
System.out.println("");
Set set=bean.getSet();
Iterator it=set.iterator();
System.out.println("Set");
while(it.hasNext())
{
String s=(String)it.next();
System.out.println(s);
}
System.out.println("");
Map map=bean.getMap();
Set keys=map.keySet();
System.out.println("Map");
Iterator it2=keys.iterator();
while(it2.hasNext())
{
String s2=(String)map.get(it2.next());
System.out.println(s2);
}
System.out.println("");
List list=bean.getList();
System.out.println("List");
for(int i=0;i<list.size();i++)
{
System.out.println(list.get(i).toString());
}
}
}
运行结果:
三、Spring Aop 编程
在Add Spring Capabilities的基础上加入spring-aop.jar
1.编写用户操作类
package com.aop;
public class UserDao {
public void addUser()
{
System.out.println("addUser");
}
public void deleteUser()
{
System.out.println("deleteUser");
}
}
2.编写检查用户安全的类
package com.aop;
import org.aspectj.lang.annotation.*;
public class CheckSecurity {
@Pointcut("execution(*add*(..))||execution(*delete*(..))")
private void allAddMethod(){};
@After("allAddMethod()")
public void check()
{
System.out.println("check ");
}
}
3.改写Spring配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<bean name="checkbean" class="com.aop.CheckSecurity"></bean>
<bean name="userDao" class="com.aop.UserDao"></bean>
</beans>
4.编写测试类
package com.aop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BeanFactory factory=new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao dao=(UserDao)factory.getBean("userDao");
dao.addUser();
dao.deleteUser();
}
}
运行结果: addUser
deleteUser
Spring 定时任务之 @Scheduled cron表达式