SSM

来源:互联网 发布:淘宝 产品对比 违规 编辑:程序博客网 时间:2024/06/11 05:48

Sping – 讲太罗嗦,只看IOC AOP和springBoot就可以了

概述

这里写图片描述
spring是现在java在开源界最火的框架
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
框架 –房屋架子
类库 – 水泥,钢筋等材料
框架

IOC

这里写图片描述
这里写图片描述

马士兵spring

反射机制:可以通过反射的api接口去探索运行期间的一个class的内部结构,并且根据它的内部结构来决定方法怎样进行调用。
依赖注入怎么实现:
读出id,id里面只要有property,看property名字是什么,生成set方法,用反射调一下,然后把bean注进来就可以。自己模拟的spring容器代码如下:

BeanFactory.javapublic interface BeanFactory {    public Object getBean(String id);}
bean.xml<beans>    <bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl" />    <bean id="userService" class="com.bjsxt.service.UserService" >        <property name="userDAO" bean="u"/>    </bean></beans>

首先把所有的bean放到容器中去,再看bean里面如果有子元素property则全部拿出来。再拼成set方法名字和方法的参数,

ClassPathXmlApplicationContext.javaimport java.lang.reflect.Method;import java.util.HashMap;import java.util.List;import java.util.Map;import org.jdom.Document;import org.jdom.Element;import org.jdom.input.SAXBuilder;public class ClassPathXmlApplicationContext implements BeanFactory {    private Map<String , Object> beans = new HashMap<String, Object>();    //IOC Inverse of Control DI Dependency Injection    public ClassPathXmlApplicationContext() throws Exception {        SAXBuilder sb=new SAXBuilder();        Document doc=sb.build(this.getClass().getClassLoader().getResourceAsStream("beans.xml")); //构造文档对象        Element root=doc.getRootElement(); //获取根元素HD        List list=root.getChildren("bean");//取名字为disk的所有元素        for(int i=0;i<list.size();i++){           Element element=(Element)list.get(i);           String id=element.getAttributeValue("id");           String clazz=element.getAttributeValue("class");           Object o = Class.forName(clazz).newInstance();           System.out.println(id);           System.out.println(clazz);           beans.put(id, o);           for(Element propertyElement : (List<Element>)element.getChildren("property")) {               String name = propertyElement.getAttributeValue("name"); //userDAO               String bean = propertyElement.getAttributeValue("bean"); //u               Object beanObject = beans.get(bean);//UserDAOImpl instance               String methodName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1);               System.out.println("method name = " + methodName);               Method m = o.getClass().getMethod(methodName, beanObject.getClass().getInterfaces()[0]);               m.invoke(o, beanObject);  // service的实例、 UserDAOImpl的实例           }        }      }    public Object getBean(String id) {        return beans.get(id);    }}

最后的测试类:

import org.junit.Test;import com.bjsxt.dao.UserDAO;import com.bjsxt.model.User;import com.bjsxt.spring.BeanFactory;import com.bjsxt.spring.ClassPathXmlApplicationContext;public class UserServiceTest {    @Test    public void testAdd() throws Exception {        BeanFactory applicationContext = new ClassPathXmlApplicationContext();        UserService service = (UserService)applicationContext.getBean("userService");        User u = new User();        u.setUsername("zhangsan");        u.setPassword("zhangsan");        service.add(u);    }}

这里写图片描述
依赖注入DI:userService里面userDao的属性是依赖容器来帮我们注入进来的,而不是写死的。
控制反转IOC:原来userDao是有当前类自己控制的,现在由容器帮忙控制,由运行上下文spring帮控制。好处:降低了耦合性,再者用配置文件注入修改起来方便而且灵活,反转到容器那里去了。

3.  IOC容器    a)  实例化具体bean    b)  动态装配4.  AOP支持    a)  安全检查    b)  管理transaction
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");建议使用ApplicationContext(接口,bean factory 的子接口)还能有 生命周期等功能

spring注入类型,

5.  <bean 中的scope属性  bean的声明周期  a)    Spring_0600_IOC_Bean_Scope  b)    singleton 单例  c)    proptotype 每次创建新的对象<bean id="u" class="com.bjsxt.dao.impl.UserDAOImpl" scope="single"/>scope="single"  -- 单例 对象1==对象2 truescope="prototype" -- 多例 对象1==对象2 false

孔浩的spring

这里写图片描述

1、引包
2、spring读取所有的文件都是在一个工厂里面读,这个工厂通过xml来配置,
原先创建类new的方式,这样依赖于实现类。

原创粉丝点击