Spring(一)入门,IOC

Spring的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: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">

  

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.username}"/>
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
    </bean>

  <!--配置-->
  <!--一。bean配置-->
   <!--二。加载外部的properties的配置 key=value-->
    <context:property-placeholder location="classpath:jdbc.properties"/>
   <!--三。组件扫描配置 确保注解能被扫描到-->
    <!--我他直接扫描最大の包-->
    <context:component-scan base-package="com"/>

</beans>

原始注解们对非自定义的bean没办法使用

pom.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">
    <parent>
        <artifactId>YES</artifactId>
        <groupId>YES</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring_anno</artifactId>

    <dependencies>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.21</version>
        </dependency>

        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>

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


    </dependencies>


</project>

properties文件里内容的使用方法 ---- ${key}

引入其他同在resource包下的xml

<import resource="User.xml">

原始注解: 替代bean配置
Spring(一)入门,IOC_第1张图片

新注解:替代扫描配置、加载properties配置、导入其他配置、方法返回值作为bean。
Spring(一)入门,IOC_第2张图片

//标志着该类是Spring的核心配置类
@Configuration
将其他注解放于Configuration之下保证被使用
//导入properties文件
@PropertySource("classpath:jdbc.properties")

//导入其他配置文件,数组
@Import({DataSourceConfiguration.class})
public class SpringConfiguration {

    @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 //返回值做bean,bean的默认名字是方法名称
    @Bean("dataSource")//自定义名字
    public DataSource getDataSource() throws PropertyVetoException {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass(driver);
        dataSource.setJdbcUrl(url);
        dataSource.setUser(username);
        dataSource.setPassword(password);
        return dataSource;
    }
}

@Configuration



//
@ComponentScan("com")//替代上边
public class DataSourceConfiguration {
}

集成Junit测试

导入依赖

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

//指定内核 进行测试
@RunWith(SpringJUnit4ClassRunner.class)

//导入配置文件进行测试
//@ContextConfiguration("classpath:ApplicationContext.xml")

//导入配置类
@ContextConfiguration(classes = SpringConfiguration.class)
public class SpringJunitTest {

    @Resource(name = "userService")
    private UserService userService;

    @Resource(name = "dataSource")
    private DataSource datasource;

    @Test
    public void test1() throws SQLException {
        userService.save();
        System.out.println(datasource.getConnection());

    }


}

你可能感兴趣的:(Spring(一)入门,IOC)