SSM_spring学习笔记

SSM_spring学习笔记

Bean实例化三种方式:

1、无参构造方法实例化

创建新的UserDaoImpl类:
package com.itssm.dao.impl;

import com.itssm.dao.UserDao;

public class UserDaoImpl implements UserDao {
    public void save() {
        System.out.println("save running..");
    }
}

Xml:
<bean id="userDao" class="com.itssm.dao.impl.UserDaoImpl"></bean>

测试类:
package com.itssm;

import com.itssm.dao.UserDao;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserDemo {
    public static void main(String[] args) {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) app.getBean("userDao");
        userDao.save();
    }
}

2、工厂静态方法实例化 创建新的静态工厂类:

package com.itssm.factory;

import com.itssm.dao.UserDao;
import com.itssm.dao.impl.UserDaoImpl;

public class StaticFactory {

    public static UserDao getUserDao(){

        return new UserDaoImpl();

    }
}

Xml文档:
<bean id="userDao" class="com.itssm.factory.StaticFactory" factory-method="getUserDao"></bean>

3、工厂实例方法实例化 创建新的工厂类DynamicFactory:

package com.itssm.factory;

import com.itssm.dao.UserDao;
import com.itssm.dao.impl.UserDaoImpl;

public class DynamicFactory {
    public  UserDao getUserDao(){

        return new UserDaoImpl();

    }
}

Xml:
<bean id="factory" class="com.itssm.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>

通过配置文件进行依赖注入 1、通过set方法进行注入

案例:UserService类中注入UserDao类

Userservice类:
package com.itssm.service.impl;

import com.itssm.dao.UserDao;
import com.itssm.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl implements UserService {

    public  UserDao userDao;
    public void setUserDao(UserDao userDao){
        this.userDao = userDao;
    }

    public void save() {
        userDao.save();
    }
}


Xml类:
<bean id="userDao" class="com.itssm.dao.impl.UserDaoImpl"></bean>
<bean id="userService" class="com.itssm.service.impl.UserServiceImpl">
    <property name="userDao" ref ="userDao"></property>
</bean>

name表示:userServiceImpl中的setUserDao方法,其中UserDao需要改为小写
Ref表示:注入到的id为userDao即上方

UserController类:
package com.itssm.demo;

import com.itssm.service.UserService;
import com.itssm.service.impl.UserServiceImpl;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserController {

    public static void main(String[] args) {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserService userservice = (UserService)app.getBean("userservice");
        userservice.save();
    }

2、p命名空间注入:

如上者基本相同,Xml修改为:
<?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="userDao" class="com.itssm.dao.impl.UserDaoImpl"></bean>-->
<!--   <bean id="userService" class="com.itssm.service.impl.UserServiceImpl">-->
<!--       <property name="userDao" ref ="userDao"></property>-->
<!--   </bean>-->

    <bean id="userService" class="com.itssm.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>
    <!--<bean id="userDao" class="com.itssm.factory.StaticFactory" factory-method="getUserDao"></bean>-->
    <!--<bean id="factory" class="com.itssm.factory.DynamicFactory"></bean>
    <bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>-->
</beans>

3、利用有参数构造方法进行注入:

UserService 修改为:
package com.itssm.service.impl;

import com.itssm.dao.UserDao;
import com.itssm.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UserServiceImpl implements UserService {

    public  UserDao userDao;
//    public void setUserDao(UserDao userDao){
//        this.userDao = userDao;
//    }
    public UserServiceImpl(UserDao userDao){
        this.userDao = userDao;
    }

    public UserServiceImpl() {
    }

    public void save() {
        userDao.save();
    }
}


Xml类:
<?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="userDao" class="com.itssm.dao.impl.UserDaoImpl"></bean>
<!--   <bean id="userService" class="com.itssm.service.impl.UserServiceImpl">-->
<!--       <property name="userDao" ref ="userDao"></property>-->
<!--   </bean>-->
<bean id="userService" class="com.itssm.service.impl.UserServiceImpl">
    <constructor-arg name="userDao" ref="userDao"></constructor-arg>
</bean>
<!--<bean id="userService" class="com.itssm.service.impl.UserServiceImpl" p:userDao-ref="userDao"/>-->
<!--<bean id="userDao" class="com.itssm.factory.StaticFactory" factory-method="getUserDao"></bean>-->
<!--<bean id="factory" class="com.itssm.factory.DynamicFactory"></bean>
<bean id="userDao" factory-bean="factory" factory-method="getUserDao"></bean>-->
</beans>

表示为用构造方法进行注入,name为service构造方法中的形参参数对象
Ref同指上述id userDao。

普通属性的注入:例setUserDao(String username)
其username注入:
Xml类:
<bean id="userDao" class="com.itssm.dao.impl.UserDaoImpl">
    <property name="username" value="zhangsan"></property>
</bean>

引用数据和集合数据注入:
Xml:
根据数据类型使用相应的子标签:
对应普通值
链表
对应key 加 value
对应propriate

Sping 引入其他配置文件(分模块开发)

**实际开发过程中,Spring的配置内容非常多,内容体积十分庞大,因此在开发过程中,我们需要有目的的将部分配置按照合适的方式进行拆解,并通过spring主配置文件的import进行加载

<import resource="applicationContext-user.xml"/>
<bean>标签
    id属性:在容器中Bean实例的唯一标识,不允许重复
    class属性:要实例化的Bean的全限定名
    scope属性:Bean的作用范围,常用是Singleton(默认)和prototype
    <property>标签:属性注入
        name属性:属性名称
        value属性:注入的普通属性值
        ref属性:注入的对象引用值
        <list>标签
        <map>标签
        <properties>标签
    <constructor-arg>标签
<import>标签:导入其他的Spring的分文件

相关API
1、ApplicatiContext实现类
1)ClassPathXmlApplicationContext
它是从类的根路径下加载配置文件 推荐使用这种
2)FileSystemXmlApplicationContext
它是从磁盘路径上加载配置文件,配置文件可以在磁盘的任意位置。
3)AnnotationConfigApplicationContext
当使用注解配置容器对象时,需要使用此类来创建 spring 容器。它用来读取注解。

数据源的手动创建(c3p0和durid):

测试类DataSourceTest:
import com.alibaba.druid.pool.DruidDataSource;
import com.mchange.v2.c3p0.DataSources;
import com.sun.javaws.security.AppContextUtil;
import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

public class DataSourceTest {

    @Test
    //手动创建数据源,测试spring容器去测试spring对象
    public void test() throws Exception {
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext1.xml");
        DataSource dataSource = app.getBean(DataSource.class);//DataSource.class特指此xml文件中代表的类的类型(容器中有唯一对象时可以使用)
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();


    }

    @Test
    public void test2() throws Exception{
        ClassPathXmlApplicationContext app = new ClassPathXmlApplicationContext("applicationContext1.xml");
        DruidDataSource dataSource = (DruidDataSource) app.getBean("dataSource2");//dataSource2特指容器中id可以在单一或多个对象中使用
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.close();
//        dataSource.setDriverClassName();
//        dataSource.setUrl();
//        dataSource.setUsername();
//        dataSource.setPassword();


    }

Xml类:

<bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean>
<bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"><!--引入不同数据源的包-->
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property><!--四个name中的元素代表了在创建数据源对象后其set方法所对应的名称及参数-->
    <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
    <property name="username" value="root"></property>
    <property name="password" value="123456"></property>
</bean>

将相关的参数配置文件拆分写入properties文件:

Jdbc.properties:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.password=123456





Xml类修改为:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context.xsd">

<!--加载外部的properti文件-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <bean id="dataSource1" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="${jdbc.driver}"></property>
    <property name="jdbcUrl" value="${jdbc.url}"></property>
    <property name="user" value="${jdbc.user}"></property>
    <property name="password" value="${jdbc.password}"></property>
    </bean>
    <bean id="dataSource2" class="com.alibaba.druid.pool.DruidDataSource"><!---->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>
</beans>

Spring原始注解:

注解	说明
   @Component	使用在类上用于实例化Bean
   @Controller	使用在web层类上用于实例化Bean
   @Service	使用在service层类上用于实例化Bean
   @Repository	使用在dao层类上用于实例化Bean
   @Autowired	使用在字段上用于根据类型依赖注入
   @Qualifier	结合@Autowired一起使用用于根据名称进行依赖注入
   @Resource	相当于@Autowired+@Qualifier,按照名称进行注入
   @Value	注入普通属性
   @Scope	标注Bean的作用范围
   @PostConstruct	使用在方法上标注该方法是Bean的初始化方法
   @PreDestroy	使用在方法上标注该方法是Bean的销毁方法

Spring新注解
使用上面的注解还不能全部替代xml配置文件,还需要使用注解替代的配置如下:

非自定义的Bean的配置:<bean>
加载properties文件的配置:<context:property-placeholder>
组件扫描的配置:<context:component-scan>
引入其他文件:<import>

注解 说明
@Configuration 用于指定当前类是一个 Spring 配置类,当创建容器时会从该类上加载注解
@ComponentScan 用于指定 Spring 在初始化容器时要扫描的包。
作用和在 Spring 的 xml 配置文件中的
一样
@Bean 使用在方法上,标注将该方法的返回值存储到 Spring 容器中
@PropertySource 用于加载.properties 文件中的配置
@Import 用于导入其他配置类

新注解开发详解:
package com.itssm.config;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;

//标志该类是Spring的核心配置类
@Configuration
//<context:component-scan base-package="com.itssm"/>
@ComponentScan("com.itssm")
//<context:property-placeholder location="classpath:jdbc.properties"/>
@PropertySource("classpath:jdbc.properties")
//<import resource=""/>
//@Import({DataSourceConfiguration.class)
public class SpringCofiguration {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;
    @Bean("dataSource")//Spring会将当前方法的返回值以指定名称存储到Spring容器当中
    public DataSource getDataSource() throws PropertyVetoException {

        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return  dataSource;
    }

}

未完待补充…

你可能感兴趣的:(intellij-idea,java,ssm)