美文网首页Spring
Spring(第一讲)

Spring(第一讲)

作者: superNeil | 来源:发表于2020-05-31 22:26 被阅读0次

重点

Spring 是什么,为什么使用它

Spring 是一个 IoC DI 和 AOP 容器框架,Spring 是一个可以帮你创建对象的,设置对象属性值的,并会存创建的对象的容器框架

解决代码高耦合度问题  IoC DI
解决事务控制繁琐问题  AOP
使用第三方框架麻烦的问题 IoC DI

什么是 IoC (控制反转)思想,Spring 与它的关系
拿来主义,要使用什么对象,不需要自己创建,交由第三方创建 , 不用 new 了 .
Spring 实现了这个 IoC 思想,换句话就是 Spring 能帮你创建对象,设置对象属性值

熟练使用 Spring IoC 功能
添加依赖
根据需求编写类
编写 Spring 的配置文件

<bean id="唯一且有意义" class="类的全限定名" [init-method="方法名" destroy-method="方法名"]>
<property name="属性名" value="属性值" | ref="容器中另外一个bean 的 id 值" />
......
</bean>
.......
启动容器
使用 Spring API 启动 ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:配置文件"); getBean...

测试常用注解 :*

​ 使用 Spring 测试启动
​ // 告诉单元测试方法运行之前启动容器
​ @RunWith(SpringJUnit4ClassRunner.class)
​ // 指定配置文件
​ @ContextConfiguration("classpath:01.hello.xml")

​ @Autowired 注解,贴测试类字段上,字段类型为你想从容器获取对象的类型

百说不如一练

练习使用 XML 配置模拟用户注册

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>cn.wolfcode</groupId>
    <artifactId>spring01-test</artifactId>
    <version>1.0.0</version>

    <dependencies>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.9</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.20</version>
            <scope>provided</scope>
        </dependency>


        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.0.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-test</artifactId>
            <version>5.0.8.RELEASE</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
public interface IStudentDao {
    //需求 : 用户注册  保存 用户名 和 密码
    void save(String username, String password) throws SQLException;
}
public class StudentDAOImpl implements IStudentDao {
    private DataSource dataSource;

    //提供 set 方法
    public void setDataSource(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Override
    public void save(String username, String password) throws SQLException {
        //获取 连接池对象  口诀  加 连 预 执 释
        @Cleanup
        Connection connection = dataSource.getConnection();
        @Cleanup
        PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO student (username , password) VALUES (?,?)");
        preparedStatement.setString(1, username);
        preparedStatement.setString(2, password);
        preparedStatement.executeUpdate();
    }
}
public interface StudentService {
    void register(String username, String password) throws SQLException;
}
public class StudentServiceImpl implements StudentService {
    private StudentDAOImpl dao;

    public void setDao(StudentDAOImpl dao) {
        this.dao = dao;
    }

    @Override
    public void register(String username, String password) throws SQLException {
        dao.save(username, password);
    }
}
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring-demo
jdbc.username=root
jdbc.password=admin
<?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: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.xsd">

    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
</bean>
    <bean id="studentDAO" class="cn.wolfcode.dao.impl.StudentDAOImpl">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <bean id="studentService" class="cn.wolfcode.service.impl.StudentServiceImpl">
        <property name="dao" ref="studentDAO"></property>
    </bean>
</beans>
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class StudentServiceImplTest {
    @Autowired
    private StudentService studentService;

    @Test
    public void testRegister() throws SQLException {
    studentService.register("root","admin");
    }
}

相关文章

网友评论

    本文标题:Spring(第一讲)

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