spring 5.0 从入门到放弃 01

来源:互联网 发布:金10数据官网财经日历 编辑:程序博客网 时间:2024/06/15 19:17

国际惯例  helloWorld奉上

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>com.ruge</groupId>  <artifactId>spring</artifactId>  <packaging>war</packaging>  <version>1.0-SNAPSHOT</version>  <name>spring Maven Webapp</name>  <url>http://maven.apache.org</url>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>    <!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->    <dependency>      <groupId>commons-logging</groupId>      <artifactId>commons-logging</artifactId>      <version>1.2</version>    </dependency>   <!--这个jar文件是所有应用都要用到的,它包含访问配置文件、创建和管理bean以及进行   Inversion of Control / Dependency Injection(IoC/DI)操作相关的所有类。如果应用只需基本的IoC/DI支持,   引入spring-core.jar及spring-beans.jar文件就可以了。-->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-beans</artifactId>      <version>5.0.0.RELEASE</version>    </dependency>    <!--这个jar文件为Spring核心提供了大量扩展。可以找到使用Spring ApplicationContext特性时所需的全部类,    JDNI所需的全部类,UI方面的用来与模板(Templating)引擎如Velocity、FreeMarker、JasperReports集成的类,    以及校验Validation方面的相关类。 -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-context</artifactId>      <version>5.0.0.RELEASE</version>    </dependency>    <!--这个jar文件包含Spring框架基本的核心工具类,Spring其它组件要都要使用到这个包里的类,是其它组件的基本核心-->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-core</artifactId>      <version>5.0.0.RELEASE</version>    </dependency>    <!-- Spring Expression Language(SpEL) -->    <dependency>      <groupId>org.springframework</groupId>      <artifactId>spring-expression</artifactId>      <version>5.0.0.RELEASE</version>    </dependency>  </dependencies>  <build>    <finalName>spring</finalName>  </build></project>

package com.ruge.test;/** * 描述: * spring的helloworld * * @outhor 爱丽丝、如歌 * @create 2017-11-01 20:50 */public class HelloWorld {    private String name;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public void hello(){        System.out.println("hello:"+name);    }}
package com.ruge.test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 描述: * main测试类 * * @outhor 爱丽丝、如歌 * @create 2017-11-01 20:52 */public class test {    public static void main(String[] args) {        HelloWorld helloWorld = new HelloWorld();        helloWorld.setName("爱丽丝、如歌");        //调用方法        helloWorld.hello();    }}

D:\download\tool\jdk\jdk8_64\install\bin\java -Didea.launcher.port=7532 -Didea.launcher.bin.path=D:\download\tool\ide\idea\2016\ideaIU-2016\install\bin -Dfile.encoding=UTF-8 -classpath D:\download\tool\jdk\jdk8_64\install\jre\lib\charsets.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\deploy.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\javaws.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jce.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfr.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jsse.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\management-agent.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\plugin.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\resources.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\rt.jar;D:\project_idea\spring\target\classes;D:\download\tool\versionControl\maven\localRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-beans\5.0.0.RELEASE\spring-beans-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-context\5.0.0.RELEASE\spring-context-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-aop\5.0.0.RELEASE\spring-aop-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-core\5.0.0.RELEASE\spring-core-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-jcl\5.0.0.RELEASE\spring-jcl-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-expression\5.0.0.RELEASE\spring-expression-5.0.0.RELEASE.jar;D:\download\tool\ide\idea\2016\ideaIU-2016\install\lib\idea_rt.jar com.intellij.rt.execution.application.AppMain com.ruge.test.testhello:爱丽丝、如歌Process finished with exit code 0

IOC与DI

  设计原则中好莱坞原则描述到,“别找我们,我们找你”,百度百科上对这点描述是“不要给我们打电话,我们会给你打电话(don‘t call us, we‘ll call you)”这是著名的好莱坞原则。在好莱坞,把简历递交给演艺公司后就只有回家等待。由演艺公司对整个娱乐项的完全控制,演员只能被动式的接受公司的差使,在需要的环节中,完成自己的演出。这一点完美的提现了在IOC身上,IOC所注重的是设计思想上,从一个常规的创建对象的做法,即new一个对象,转变成向IOC容器递交”简历“,被动的等待IOC容器返回资源给你。控制反转即指的是”演艺公司控制演员“,而说到依赖,则是“演员需要公司混饭”,我们所需求的对象,需要依赖容器来获得,这个过程即是依赖注入。本质上IOC和DI是同一思想下不同维度的表现


<?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.xsd">    <!-- 配置需要被Spring管理的Bean(创建,创建后放在了Spring IOC容器里面)-->    <bean id="hello" class="com.ruge.test.HelloWorld">        <!-- 配置该Bean需要注入的属性(是通过属性set方法来注入的) -->        <property name="name" value="刘德华"></property>    </bean></beans>

package com.ruge.test;        import org.springframework.context.ApplicationContext;        import org.springframework.context.support.ClassPathXmlApplicationContext;/** * 描述: * main测试类 * * @outhor 爱丽丝、如歌 * @create 2017-11-01 20:52 */public class test {    public static void main(String[] args) {        //获取Spring的ApplicationContext配置文件,注入IOC容器中        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");        HelloWorld helloWorld = (HelloWorld) applicationContext.getBean("hello");        //调用方法        helloWorld.hello();    }}

D:\download\tool\jdk\jdk8_64\install\bin\java -Didea.launcher.port=7534 -Didea.launcher.bin.path=D:\download\tool\ide\idea\2016\ideaIU-2016\install\bin -Dfile.encoding=UTF-8 -classpath D:\download\tool\jdk\jdk8_64\install\jre\lib\charsets.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\deploy.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\access-bridge-64.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\cldrdata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\dnsns.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jaccess.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\jfxrt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\localedata.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\nashorn.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunec.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunjce_provider.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunmscapi.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\sunpkcs11.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\ext\zipfs.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\javaws.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jce.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfr.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jfxswt.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\jsse.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\management-agent.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\plugin.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\resources.jar;D:\download\tool\jdk\jdk8_64\install\jre\lib\rt.jar;D:\project_idea\spring\target\classes;D:\download\tool\versionControl\maven\localRepository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-beans\5.0.0.RELEASE\spring-beans-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-context\5.0.0.RELEASE\spring-context-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-aop\5.0.0.RELEASE\spring-aop-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-core\5.0.0.RELEASE\spring-core-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-jcl\5.0.0.RELEASE\spring-jcl-5.0.0.RELEASE.jar;D:\download\tool\versionControl\maven\localRepository\org\springframework\spring-expression\5.0.0.RELEASE\spring-expression-5.0.0.RELEASE.jar;D:\download\tool\ide\idea\2016\ideaIU-2016\install\lib\idea_rt.jar com.intellij.rt.execution.application.AppMain com.ruge.test.test十一月 01, 2017 9:30:56 下午 org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5387f9e0: startup date [Wed Nov 01 21:30:56 CST 2017]; root of context hierarchy十一月 01, 2017 9:30:57 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions信息: Loading XML bean definitions from class path resource [applicationContext.xml]hello:刘德华Process finished with exit code 0