标签:https 实现 表示 编写 实现类 style 数据库 str creat
/** * @Author:jimisun * @Description: * @Date:Created in 20:32 2018-09-26 * @Modified By: */ public class HelloSpringBean { public String sayHello() { return "Hello Spring!!!"; } }
你可以看到,这就是一个POJO(简单的JAVA类),没有任何地方表明它是Spring组件,Spring非侵入式编程模型意味着这个类在Spring应用和非Spring应用中都可以发挥同样的作用。尽管看起来很简单;但Spring通过IOC(Inversion of Control)管理这个POJO,然后通过DI(Dependency Inject)来注入他们,这个POJO就变的魔力十足;那么DI是如何帮助应用对象彼此之间保持松耦合的呢?
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Gril { private EatAction eat;
public BeautifulGirl() { this.eat = new EatAction(); } @Override public void action() { eat.action(); } }
在BeautifulGirl(可爱的小女孩)这个类中,在构造方法中创建一个EatAction(吃饭动作)。这样就极大限制了BeautifulGirl(可爱的小女孩)的能力;如果现在小女孩需要去玩耍呢?或者需要去睡觉呢?真是太抱歉了,BeautifulGirl(可爱的小女孩)只会吃东西这个动作。这是什么原因呢?这就是BeautifulGirl(可爱的小女孩)和EatAction(吃饭动作)这两个类紧紧耦合在了一起!紧密耦合同时会造成代码的难以测试,难以服用,难以理解,并且典型地表现出"打地鼠“式的Bug特性(修复一个Bug,将会出现一个或多个新Bug),所以我们可以知道耦合是必须的,但必须谨慎管理耦合,但是怎么才算是谨慎处理耦合关系呢?
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Gril { private Action action; public BeautifulGirl(Action action) { this.action = action; } @Override public void action() { action.action(); } }
从上面实例代码中可以看到BeautifulGirl本身并没有创建任何的动作,而是通过构造方法传入一个实现了Action(动作)接口的实现类即可,也就是说BeautifulGirl可以完成任意实现了Action接口的动作(睡觉啦...玩耍啦...旅行啦....)。这里的要点是BeautifulGirl没有与任何特定的Action发生耦合。BeautifulGirl只需要的是一个实现Action接口就行,对象本身只是通过接口(而非具体实现或初始化过程)来表明依赖关系,那么这种依赖就能够在BeautifulGirl不知情的情况下替换不同的具体动作。好了我们现在明白了DI进行依关系解耦的原理了,下面我们看一下如何在Spring中应用DI。 example4实例源码下载
<?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/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean class="com.jimisun.spring.example4.BeautifulGirl" id="beautifulGirl"> <constructor-arg ref="action"/> </bean> <bean class="com.jimisun.spring.example4.SleepAction" id="action"></bean> </beans>
/** * @Author:jimisun * @Description: * @Date:Created in 07:53 2018-09-27 * @Modified By: */ public class Main { public static void main(String[] args) { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("Spring.xml"); BeautifulGirl beautifulGirl = (BeautifulGirl) context.getBean("beautifulGirl"); beautifulGirl.action(); context.close(); } }
这样执行Main方法,从Context中获取BeautifulGirl实例执行action方法。当然Spring提供了基于Java的配置,可作为XML配置文件的代替方案 example5实例源码下载
/** * @Author:jimisun * @Description: * @Date:Created in 08:40 2018-09-27 * @Modified By: */ @Configuration public class SpringConfig { @Bean public SleepAction sleepAction() { return new SleepAction(); } @Bean public BeautifulGirl beautifulGirl() { return new BeautifulGirl(sleepAction()); } }
/** * @Author:jimisun * @Description: * @Date:Created in 07:53 2018-09-27 * @Modified By: */ public class Main { public static void main(String[] args) { ApplicationContext applicationContext = new AnnotationConfigApplicationContext(SpringConfig.class); SleepAction action = applicationContext.getBean(SleepAction.class); action.action(); } }
/** * @Author:jimisun * @Description: * @Date:Created in 09:32 2018-09-27 * @Modified By: */ public class Parent { public void check() { System.out.println("检查动作是否安全......."); } }
非常简单!Parent(家长类)只有一个方法就是check,那么现在就让Parent对BeautifulGirl的执行动作进行检查吧。
<bean class="com.jimisun.spring.example6.BeautifulGirl" id="beautifulGirl"> <constructor-arg ref="action"/> <constructor-arg ref="parent"/> </bean> <bean class="com.jimisun.spring.example6.SleepAction" id="action"></bean> <bean class="com.jimisun.spring.example6.Parent" id="parent"></bean> </beans>
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Girl { private Action action; private Parent parent; public BeautifulGirl(Action action, Parent parent) { this.action = action; this.parent = parent; } @Override public void action() { parent.check(); action.action(); } }
<!--声明Bean--> <bean class="com.jimisun.spring.example7.Parent" id="parent"></bean> <!--声明切面--> <aop:config> <aop:aspect ref="parent"> <aop:pointcut id="girlAction" expression="execution(* com.jimisun.spring.example7.Action.*(..))"/> <aop:before pointcut-ref="girlAction" method="check"/> </aop:aspect> </aop:config>
/** * @Author:jimisun * @Description: * @Date:Created in 07:44 2018-09-27 * @Modified By: */ public class BeautifulGirl implements Girl { private Action action; public BeautifulGirl(Action action) { this.action = action; } @Override public void girlAction() { action.action(); } }
/** * @Author:jimisun * @Description: * @Date:Created in 11:13 2018-09-27 * @Modified By: */ public class Main { public static void main(String[] args) { JdbcTemplate jdbcTemplate = new JdbcTemplate(); jdbcTemplate.execute("select * from user"); } }
标签:https 实现 表示 编写 实现类 style 数据库 str creat
原文地址:https://www.cnblogs.com/jimisun/p/9712174.html