美文网首页
JavaWeb Spring

JavaWeb Spring

作者: dawsonenjoy | 来源:发表于2019-04-25 21:46 被阅读0次

IoC

Inverse of Control,控制反转,即原来对于对象的实例化都是通过new实现,现在可以通过spring实现

使用步骤

1.导入相关jar包,主要有:spring-aop(开发AOP特性需要的jar)、spring-beans(处理Bean的jar)、spring-context(处理上下文的jar)、spring-core(核心jar)、spring-expression(spring表达式的jar)、commons-logging(日志相关jar)
2.编写目标类,举例:

public class BeanTest {
    private int id;
    private String name;
    private String pwd;
    public BeanTest(){}
    // 如果有有参构造方法,就也要有无参构造方法
    public BeanTest(int id, String name, String pwd) {
        super();
        this.id = id;
        this.name = name;
        this.pwd = pwd;
    }
    ...
    // getter/setter方法
    @Override
    public String toString() {
        return id + "-" + name + "-" + pwd;
    }

}

3.进行文件配置,配置文件一般在classpath(src)下,一般取名:applicationContext.xml,在文件当中设置schema约束,然后在xml文件当中设置<bean>标签,里面要有idclass属性,举例:

<?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">

<!-- bean对象 -->
<bean id="beanTestId" class="com.aaa.BeanTest">
<!-- 可以理解成:beanTest xx = new BeanTest() -->
    <!-- 设置属性值 -->
    <property name="id" value="1"></property>
    <!-- 可以理解成:xx.setId("1") -->
    <property name="name" value="aaa"></property>
    <property name="pwd" value="bbb"></property>
</bean>

</beans>

注:
对于给属性赋值时,有两种方式:一种是在<property>标签里通过value属性赋值,另一种则是在<property>标签里嵌套子标签<value>来进行属性赋值。相比之下,主要区别如下:
1.后者可以添加双引号
2.后者可以通过type属性来设置值的类型
3.对于</&这些符号,前者只能使用&lt;/&amp;这种来代替,而后者还可以通过<![CDATA[字符串]]>来输入任意字符
举例:

<property name="id">
    <value type="int">1</value>
    <!-- int类型值 -->
</property>
<property name="name">
    <value type="java.lang.String">a<![CDATA[a<!>]]>a</value>
    <!-- 对于类属性,需要写全类的位置 -->
</property>
<property name="pwd" value="bbb"></property>

其中,对于值为null的情况,有<null/>标签进行设置,举例:

<property name="name">
    <null/>
    <!-- 设置值为null时不需要value标签 -->
</property>

注2:
如果在类A的property当中要设置的属性值不属于基本数据类型,比如要设置成一个类,那么应该把该类也加进bean工厂当中,并在类A中的property里设置ref而不是value属性,值为对应类的beanId,举例:

<bean id="beanTestId" class="com.aaa.BeanTest">
    <property name="id" value="1"></property>
    <property name="name" value="aaa"></property>
    <property name="pwd" value="bbb"></property>
</bean>
<bean id="xxxId" class="com.aaa.xxx">
    <property name="bean" ref="beanTestId"></property>
    <!-- 使用ref,设置值为上面的类id -->
</bean>

这个也称为setter依赖注入,后面会进行介绍
4.进行测试,代码示例:

public class Test {
    public static void main(String[] args) {
//      原来调用
//      BeanTest bt1 = new BeanTest(1,"aaa", "bbb");
//      System.out.println(bt1.toString());
        
//      spring方式调用
//      获取配置容器
        String xmlPath = "com/aaa/beans.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);
//      获取bean对象
        BeanTest bt = (BeanTest) bf.getBean("beanTestId");
        System.out.println(bt.toString());
    }
}

DI

Dependency Injection,依赖注入,即通过spring的setter方法来设置(IOC改名来的,两个相当于一回事)。

依赖

假如有类:

class B{
    private A a;
}

那么就称B类依赖于A类,即一个类需要使用到另一个类时则称为依赖

注入

在类中设置另一个类的实例,可以通过setter方法、构造器

步骤举例(setter注入)

1.创建service实例
2.创建dao实例
3.通过setter方法将dao设置给service,代码示例:

public class ServiceTestImpl implements ServiceTest{    
    public DaoTestImpl daoTest;
    // 不使用new,而是通过setter方法进行设置
    public void setDaoTest(DaoTestImpl daoTest){
        this.daoTest = daoTest;
    }
    
    @Override
    public void aaa() {
        this.daoTest.ddd();
    }
}

4.在配置文件当中进行service和dao的配置,举例:

<bean id="testDao" class="com.aaa.DaoTestImpl"></bean>
<!-- 配置dao -->
<bean id="testService" class="com.aaa.ServiceTestImpl">
<!-- 配置service -->
    <property name="daoTest" ref="testDao"></property>
    <!-- name是dao中的属性,ref是dao的id -->
</bean>

5.测试,举例:

public class Test {
    public static void main(String[] args) {
//      获取配置容器
        String xmlPath = "com/aaa/beans.xml";
        ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);
//      获取bean对象
        ServiceTestImpl dt = (ServiceTestImpl)ac.getBean("testService");
        dt.aaa();
    }
}

注:
对于列表、集合等数据类型的注入,有对应的标签可以使用,举例:
bean类:

public class BeanCollectionTest {
    private List<String> listTest;
    private String[] arrayTest;
    private Set<String> setTest;
    private Map<String, String> mapTest;
    ...
    // getter/setter方法
    public void show() {
        for(String s: listTest){
            System.out.println("list:" + s);
        }
        System.out.println(Arrays.toString(arrayTest));
        for(String s: setTest){
            System.out.println("set:" + s);
        }
        for(String s: mapTest.keySet()){
            System.out.println(s + ":" + mapTest.get(s));
        }
    }
}

配置文件:

<bean id="BeanCollectionTestId" class="com.aaa.BeanCollectionTest">
    <property name="listTest">
        <list>
            <value>aaa</value>
            <value>bbb</value>
            <value>bbb</value>
        </list>
    </property>
    <property name="arrayTest">
        <array>
            <value>aaa</value>
        </array>
    </property>
    <property name="setTest">
        <set>
            <value>aaa</value>
        </set>
    </property>
    <property name="mapTest">
        <map>
            <entry>
                <key>
                    <value>aaa</value>
                </key>
                <value>111</value>
            </entry>
            <entry>
                <key>
                    <value>bbb</value>
                </key>
                <value>222</value>
            </entry>
        </map>
    </property>
</bean>
构造器注入

在bean配置中通过<property>标签设置属性注入,而如果要用构造器注入,则需要先在bean当中加入构造方法,并在配置文件当中使用<constructor-arg>标签设置,举例:

<bean id="beanTestId" class="com.aaa.BeanTest">
    <constructor-arg name="id" value="1"></constructor-arg>
    <constructor-arg name="name" value="aaa"></constructor-arg>
    <constructor-arg name="pwd" value="bbb" type="String"></constructor-arg>
</bean>
p命名空间注入

需要先在xml文件中导入:xmlns:p="http://www.springframework.org/schema/p",然后在<bean>标签中通过p:属性名/p:属性名-ref(值为非基础类型时)来设置属性,举例:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="beanTestId" class="com.aaa.BeanTest" p:id="1" p:name="aaa" p:pwd="bbb"></bean>
<!-- 通过p命名空间注入 -->
</beans>

注解添加Bean

前面是通过在配置文件当中定义bean来将对象加入Ioc容器当中,而使用注解也可以达到同样的效果,步骤:
1.在bean类里添加Component注解,并设置id值,举例:

@Component("beanTestId")
public class BeanTest {
    ...
}

2.在配置文件当中使用<context:component-scan>标签扫描指定路径下的bean,需要先引入下面的规范:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">

扫描器如下:

<context:component-scan base-package="com.aaa"></context:component-scan>

XML加载

主要提供了2类加载方式:BeanFactory、ApplicationContext

ApplicationContext

当对xml文件进行加载时就会实例化里面的bean对象,前面的测试例子用的就是这种加载方式(使用了ClassPathXmlApplicationContext实现类,还有FileSystemXmlApplicationContext实现类)

BeanFactory

和上面的加载方式不同的是,当使用getBean方法时才会实例化bean对象,举例:

public class Test {
    public static void main(String[] args) {
        String xmlPath = "com/aaa/beans.xml";
        BeanFactory bf = new XmlBeanFactory(new ClassPathResource(xmlPath));
        ServiceTestImpl dt = (ServiceTestImpl)bf.getBean("testService");
        //此时才实例化
        dt.aaa();
    }
}

AOP

Aspect Oriented Programming,面向切面编程

Web开发

基于Spring的Web开发,需要在导入前面的六个jar包基础上,再导入一个spring-web的jar包

初始化Ioc容器

对于原来的java 项目,往往初始化Ioc容器是通过下面语句进行初始化:

ApplicationContext ac = new ClassPathXmlApplicationContext(xmlPath);

而在web项目中,不可能每次访问时都new一个Ioc容器,因此需要在web.xml配置文件当中进行监听器的配置,使得web项目在启动时将会自动初始化Ioc容器,配置示例如下:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <!-- 配置该参数,值为对应的配置文件名,此时代表在src下,放在WEB-INF下就改成:WEB-INF/applicationContext.xml -->
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>

如果想要查看Ioc容器是否初始化成功,可以添加下面的监听器来实现,若失败将会报错:

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

注:
如果有多个配置文件,那么可以都放在对应的<param-value>标签下,用,隔开

相关文章

网友评论

      本文标题:JavaWeb Spring

      本文链接:https://www.haomeiwen.com/subject/sisvwqtx.html