关闭

Spring.No1 ----整理---Spring学习的第一课

标签: Spring框架javaweb
163人阅读 评论(0) 收藏 举报
分类:

1.Spring(业务层)重点知识

   IOC(Invertion of Control)

   DI(依赖注入)

   AOP(面向切面)

2.applicationContext配置 

<bean id=" "  class=" " >

setter注入   

      <property name=" "  value=" "/ref=" ">

构造方法注入

      <constructor-args index=" "  value=" ">

一 、关于AOP实例

      1.jar包导入

  

2.建立接口和实现类

public interface IUserDAO {
void saveStudent(String name);
void deleteStudent(String name);
void updateStudent(String name);
void findStudent(String name);
}

public class UserDAOImpl implements IUserDAO{
@Override
public void saveStudent(String name) {
// TODO Auto-generated method stub
System.out.println("正在保存信息"+name);
}
@Override
public void deleteStudent(String name) {
// TODO Auto-generated method stub
System.out.println("正在删除信息"+name);
}
@Override
public void updateStudent(String name) {
// TODO Auto-generated method stub
System.out.println("正在修改信息"+name);
}
@Override
public void findStudent(String name) {
// TODO Auto-generated method stub
System.out.println("正在查询信息"+name);
}
}

3.创建com.proxy包,放“切面”类(日志,权限等)

日志类

public class UserDAOAdvice implements MethodBeforeAdvice, AfterReturningAdvice {
private static Logger logger = Logger.getLogger(UserDAOAdvice.class);

@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
// TODO Auto-generated method stub
logger.info("操作开始----------");
}
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
// TODO Auto-generated method stub
logger.info("操作结束----------");
}
}

权限控制

public class PrivilegeAdvice implements MethodInterceptor{
private static Logger logger = Logger.getLogger(PrivilegeAdvice.class);
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
// TODO Auto-generated method stub
String name = invocation.getMethod().getName();
if (name.equals("findStudent")) {
return invocation.proceed();
}else{
throw new Exception("你没有权限!!");
}
}
}

4.配置applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
    "http://www.springframework.org/dtd/spring-beans-2.0.dtd">


<beans>

         代理目标
<bean id="userDAOImpl" class="com.dao.impl.UserDAOImpl"></bean>
          建议拦截对象类
<bean id="logAdvice" class="com.proxy.UserDAOAdvice"></bean>
<bean id="priAdvice" class="com.proxy.PrivilegeAdvice"></bean>

           通过ProxyFactoryBean创建代理类
    <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean">

           代理类
        <property name="target" ref="userDAOImpl"></property>

            接口
        <property name="interfaces">
            <list>
               <value>com.dao.IUserDAO</value>
            </list>
        </property>

           拦截器
        <property name="interceptorNames">
            <list>
               <value>logAdvice</value>
               <value>priAdvice</value>
            </list>
        </property> 
    </bean>
</beans>

5.创建测试类

public class TestUser {
@Test
public void test() {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDAO dao = (IUserDAO)app.getBean("proxy");
try {

dao.findStudent("艾斯");
dao.deleteStudent("乔巴");
dao.saveStudent("路飞");
} catch (Exception e) {
// TODO Auto-generated catch block
System.out.println("你没有权限");
}
}
}

二、关于DI依赖注入+控制反转实例

    1.jar包导入

    2.创建接口Pet及继承类cat,dog,创建Boy类,包含pet 对象

public interface Pet {
void eat();
}

public class Dog implements Pet {
private String name;
private int age;

@Override
public void eat() {
System.out.println(name+age+"吃肉");
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

public class Boy {
private String name;
private Pet pet;
private static Logger logger = Logger.getLogger(Boy.class);

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Pet getPet() {
return pet;
}
public void setPet(Pet pet) {
this.pet = pet;
}
public void feedPet() {
logger.info("开始喂食");
System.out.print(name+"喂");
pet.eat();
logger.info("喂食结束");
}
}

三、进行applicationContext.xml的配置

<beans>
<bean id="helloService" class="com.service.HelloService"></bean>
<bean id="cat" class="com.po.Cat">
<property name="name" value="tom" />
<property name="age" value="8" />
</bean>

<bean id="dog" class="com.po.Dog">
<property name="name" value="wangwang" />
<property name="age" value="10" />
</bean>
Setter方式注入
<bean id="boy" class="com.po.Boy">
<property name="name" value="baobao" /> <!-- 普通属性赋值 -->
<property name="pet" ref="dog"></property>   <!-- 另一个对象:DI依赖注入 + 控制反转 -->
</bean>

构造器注入

       <bean id="boy" class="com.po.Boy">
<property name="name" value="baobao" /> <!-- 普通属性赋值 -->
<constructor-arg index=" " value="  " ></property>   <!-- 另一个对象:DI依赖注入 + 控制反转 -->
</bean>

四、创建测试类进行测试

@Test
public void test() {
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Boy boy = (Boy) ctx.getBean("boy");
boy.feedPet();
}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:2346次
    • 积分:94
    • 等级:
    • 排名:千里之外
    • 原创:7篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档