spring

1.创建bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
    <bean id="userController" class="com.augmentum.oes.controller.UserController" scope="singleton"></bean>
    <bean id="questionServiceImpl" class="com.augmentum.oes.service.impl.QuestionServiceImpl" scope="singleton"></bean>
    <bean id="userDaoImpl" class="com.augmentum.oes.dao.impl.UserDaoImpl" scope="singleton"></bean>
    
    
    <bean id="questionController" class="com.augmentum.oes.controller.QuestionController" scope="singleton">
        <property name="questionServiceImpl" ref="questionServiceImpl" value=""/>
    </bean>
    <bean id="questionServiceImpl" class="com.augmentum.oes.service.impl.QuestionServiceImpl" scope="singleton">
        <property name="questionDaoImpl" ref="questionDaoImpl"/>
    </bean>
    <bean id="questionDaoImpl" class="com.augmentum.oes.dao.impl.QuestionDaoImpl" scope="singleton">
        <property name="jdbcTemplete" ref="jdbcTemplete"/>
    </bean>
    <bean id="jdbcTemplete" class="com.augmentum.oes.common.JDBCTemplete" scope="singleton" ></bean>
</beans>
bean.xml

2.存储model

package com.augmentum.oes.common;

import java.util.ArrayList;
import java.util.List;

public class BeanConfig {
    private String id;
    private String className;
    private String scope;

    private List<BeanProperty> properties = new ArrayList<BeanProperty>();

    public void addProperty(BeanProperty property) {
        properties.add(property);
    }

    public List<BeanProperty> getProperties() {
        return properties;
    }

    public void setProperties(List<BeanProperty> properties) {
        if (properties == null) {
            properties = new ArrayList<BeanProperty>();
        }
        this.properties = properties;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

}
BeanConfig
package com.augmentum.oes.common;

public class BeanProperty {
    private String name;
    private String value;
    private String ref;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getRef() {
        return ref;
    }

    public void setRef(String ref) {
        this.ref = ref;
    }

}
BeanProperty

3.创建工厂

package com.augmentum.oes.common;

import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.augmentum.oes.servlet.DispatcherServlet;

public class BeanFactory {
    private static Map<String, BeanConfig> beans = new HashMap<String, BeanConfig>();

    private static Map<String, Object> objects = new HashMap<String, Object>();

    private static BeanFactory beanFactory;

    public Object getBean(String id) {
        if (beans.containsKey(id)) {
            BeanConfig bean = beans.get(id);
            String scope = bean.getScope();
            if (scope == null || scope.equals("")) {
                scope = "singleton";
            }
            if (scope.equalsIgnoreCase("singleton")) {
                if (objects.containsKey(id)) {
                    return objects.get(id);
                }
            }

            String className = bean.getClassName();
            Class<?> clz = null;
            try {
                clz = Class.forName(className);
                Object object = clz.newInstance();

                if (scope.equalsIgnoreCase("singleton")) {
                    objects.put(id, object);
                }
                List<BeanProperty> beanProperties = bean.getProperties();

                if (beanProperties != null || !beanProperties.isEmpty()) {
                    for (BeanProperty beanProperty : beanProperties) {
                        String name = beanProperty.getName();
                        String fistChar = name.substring(0,1);
                        String leaveChar = name.substring(1);
                        String methodName ="set"+ fistChar.toUpperCase()+leaveChar;

                        Method method = null;
                        Method[] methods = clz.getMethods();
                        //find in object method
                        for (Method methodInClass : methods) {
                            String methodNameInClass = methodInClass.getName();
                            if (methodNameInClass.equals(methodName)) {
                                method = methodInClass;
                                break;
                            }
                        }

                        String ref = beanProperty.getRef();
                        String value = beanProperty.getValue();
                        if (ref != null && !ref.trim().equals("")) {
                            //digui  get this bean.xml have bean set Object
                            Object refObj = this.getBean(ref);
                            method.invoke(object, refObj);
                        } else if (value != null && !value.trim().equals("")) {
                            Class<?>[] parmts = method.getParameterTypes();
                            String propertyValue = beanProperty.getValue();
                            if (parmts[0]==String.class) {
                                method.invoke(object, propertyValue);
                            }
                            if (parmts[0]==int.class) {
                                method.invoke(object, Integer.parseInt(propertyValue));
                            }
                            if (parmts[0]==boolean.class) {
                                method.invoke(object, Boolean.parseBoolean(propertyValue));
                            }
                        }
                    }
                }

                return object;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    private BeanFactory(){}
    //thread  join
    public static BeanFactory getInstance() {
        if (beanFactory == null) {
            beanFactory = new BeanFactory();
            beanFactory.init();
        }
        return beanFactory;
    }

    private void init() {
        InputStream inputStream = null;
        try {
            inputStream = DispatcherServlet.class.getClassLoader().getResourceAsStream("bean.xml");

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(inputStream);
            Element element = document.getDocumentElement();

            NodeList beanNodes = element.getElementsByTagName("bean");
            if (beanNodes == null) {
                return;
            }
            int beanLength = beanNodes.getLength();
            for (int i = 0; i < beanLength; i++) {
                Element beanElement = (Element) beanNodes.item(i);
                BeanConfig bean = new BeanConfig();
                String id = beanElement.getAttribute("id");
                bean.setId(id);
                String className = beanElement.getAttribute("class");
                bean.setClassName(className);
                String scope = beanElement.getAttribute("scope");
                bean.setScope(scope);

                beans.put(id, bean);

                NodeList beanPropertyNodes = beanElement.getElementsByTagName("property");

                if (beanPropertyNodes != null|| !beanPropertyNodes.equals("")) {
                    int beanPropertyLength = beanPropertyNodes.getLength();
                    for (int j = 0; j < beanPropertyLength; j++) {
                        Element beanPropertyElement = (Element) beanPropertyNodes.item(j);
                        BeanProperty beanProperty = new BeanProperty();
                        beanProperty.setName(beanPropertyElement.getAttribute("name"));
                        beanProperty.setRef(beanPropertyElement.getAttribute("ref"));
                        beanProperty.setValue(beanPropertyElement.getAttribute("value"));

                        bean.addProperty(beanProperty);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
BeanFactory

4.set注入

ApplicationContext的主要实现类
[1]ClassPathXmlApplicationContext:对应类路径下的XML格式的配置文件
[2]FileSystemXmlApplicationContext:对应文件系统中的XML格式的配置文件
 
 <context:property-placeholder location="classpath:jdbc.properties"/>
 
注解开发 :
①普通组件:@Component
标识一个受Spring IOC容器管理的组件
②持久化层组件:@Respository
标识一个受Spring IOC容器管理的持久化层组件
③业务逻辑层组件:@Service
标识一个受Spring IOC容器管理的业务逻辑层组件
④表述层控制器组件:@Controller
标识一个受Spring IOC容器管理的表述层控制器组件
⑤组件命名规则
[1]默认情况:使用组件的简单类名首字母小写后得到的字符串作为bean的id
[2]使用组件注解的value属性指定bean的id
<context:component-scan base-package="com.atguigu.component" esource-pattern="autowire/*.class"/>注解使用的包  esource-pattern过滤扫描特定的类
<context:include-filter> 子节点表示要包含的目标类 <context:exclude-filter>子节点表示要排除在外的目标类
 

二.  AOP  

<aop:aspectj-autoproxy>
当Spring IOC容器侦测到bean配置文件中的<aop:aspectj-autoproxy>元素时,会自动为与AspectJ切面匹配的bean创建代理
 
用AspectJ注解声明切面
①要在Spring中声明AspectJ切面,只需要在IOC容器中将切面声明为bean实例。②当在Spring IOC容器中初始化AspectJ切面之后,Spring IOC容器就会为那些与 AspectJ切面相匹配的bean创建代理。
③在AspectJ注解中,切面只是一个带有@Aspect注解的Java类,它往往要包含很多通知。
④通知是标注有某种注解的简单的Java方法。
⑤AspectJ支持5种类型的通知注解:
[1]@Before:前置通知,在方法执行之前执行
[2]@After:后置通知,在方法执行之后执行
[3]@AfterRunning:返回通知,在方法返回结果之后执行
[4]@AfterThrowing:异常通知,在方法抛出异常之后执行
[5]@Around:环绕通知,围绕着方法执行
注解切面类
 
execution([权限修饰符] [返回值类型] [简单类名/全类名] [方法名]([参数列表]))  
● 返回通知:无论连接点是正常返回还是抛出异常,后置通知都会执行。如果只想在连接点返回的时候记录日志,应使用返回通知代替后置通知。 ● 使用@AfterReturning注解 ● 在返回通知中访问连接点的返回值 ● 在返回通知中,只要将returning属性添加到@AfterReturning注解中,就可以访问连接点的返回值。该属性的值即为用来传入返回值的参数名称 ● 必须在通知方法的签名中添加一个同名参数。在运行时Spring AOP会通过这个参数传递返回值 ● 原始的切点表达式需要出现在pointcut属性中

  

  ● 异常通知:只在连接点抛出异常时才执行异常通知

  

对于多个切点类作用于一个类上,可以设置优先级@Order(0)@Order(1)

 xml中配置

先定义在引入<aop:aspect>中

 <aop:config>
        <aop:pointcut expression="execution(* com.augmentum.oes.service..*.*(..))" id="pc"/>
        <aop:advisor pointcut-ref="pc" advice-ref="txAdvice" order="1"/>
    </aop:config>

声明切入点
  ● 切入点使用<aop:pointcut>元素声明。
  ● 切入点必须定义在<aop:aspect>元素下,或者直接定义在<aop:config>元素下。
  ● 定义在<aop:aspect>元素下:只对当前切面有效
  ● 定义在<aop:config>元素下:对所有切面都有效
  ● 基于XML的AOP配置不允许在切入点表达式中用名称引用其他切入点。

声明通知

 17:06:372017-07-29

参考实现链接 http://www.cnblogs.com/davidwang456/p/4013631.html
 
 
 

 

原文地址:https://www.cnblogs.com/mxz1994/p/7245750.html