Spring(第一講)

重點(diǎn)

Spring 是什么,為什么使用它

Spring 是一個(gè) IoC DI 和 AOP 容器框架,Spring 是一個(gè)可以幫你創(chuàng)建對(duì)象的,設(shè)置對(duì)象屬性值的,并會(huì)存創(chuàng)建的對(duì)象的容器框架

解決代碼高耦合度問題  IoC DI
解決事務(wù)控制繁瑣問題  AOP
使用第三方框架麻煩的問題 IoC DI

什么是 IoC (控制反轉(zhuǎn))思想,Spring 與它的關(guān)系
拿來主義,要使用什么對(duì)象,不需要自己創(chuàng)建,交由第三方創(chuàng)建 , 不用 new 了 .
Spring 實(shí)現(xiàn)了這個(gè) IoC 思想,換句話就是 Spring 能幫你創(chuàng)建對(duì)象,設(shè)置對(duì)象屬性值

熟練使用 Spring IoC 功能
添加依賴
根據(jù)需求編寫類
編寫 Spring 的配置文件

<bean id="唯一且有意義" class="類的全限定名" [init-method="方法名" destroy-method="方法名"]>
<property name="屬性名" value="屬性值" | ref="容器中另外一個(gè)bean 的 id 值" />
......
</bean>
.......
啟動(dòng)容器
使用 Spring API 啟動(dòng) ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:配置文件"); getBean...

? 測(cè)試常用注解 :*

? 使用 Spring 測(cè)試啟動(dòng)
? // 告訴單元測(cè)試方法運(yùn)行之前啟動(dòng)容器
? @RunWith(SpringJUnit4ClassRunner.class)
? // 指定配置文件
? @ContextConfiguration("classpath:01.hello.xml")

? @Autowired 注解,貼測(cè)試類字段上,字段類型為你想從容器獲取對(duì)象的類型

百說不如一練

練習(xí)使用 XML 配置模擬用戶注冊(cè)

<?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 {
    //需求 : 用戶注冊(cè)  保存 用戶名 和 密碼
    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 {
        //獲取 連接池對(duì)象  口訣  加 連 預(yù) 執(zhí) 釋
        @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");
    }
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容