日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Spring數(shù)據(jù)源及注解開發(fā)

作者:學(xué)JAVA的秀琴 更新時(shí)間: 2022-05-11 編程語言

目錄

1.數(shù)據(jù)源的手動(dòng)創(chuàng)建

Spring配置數(shù)據(jù)源

2. Spring注解開發(fā)

1 Spring原始注解

2 Spring新注解

Spring整合Juni

1.數(shù)據(jù)源的手動(dòng)創(chuàng)建

常見的數(shù)據(jù)源(連接池):DBCP、C3P0、BoneCP、Druid等

開發(fā)步驟

①導(dǎo)入數(shù)據(jù)源的坐標(biāo)和數(shù)據(jù)庫驅(qū)動(dòng)坐標(biāo)

②創(chuàng)建數(shù)據(jù)源對(duì)象

③設(shè)置數(shù)據(jù)源的基本連接數(shù)據(jù)

④使用數(shù)據(jù)源獲取連接資源和歸還連接資源

①導(dǎo)入c3p0和druid的坐標(biāo)



    c3p0
    c3p0
    0.9.1.2



    com.alibaba
    druid
    1.1.10

①導(dǎo)入mysql數(shù)據(jù)庫驅(qū)動(dòng)坐標(biāo)



    mysql
    mysql-connector-java
    5.1.39

②創(chuàng)建C3P0連接池

@Test
public void testC3P0() throws Exception {
	//創(chuàng)建數(shù)據(jù)源
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	//設(shè)置數(shù)據(jù)庫連接參數(shù)
    dataSource.setDriverClass("com.mysql.jdbc.Driver");    	               	               dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
    dataSource.setUser("root");
    dataSource.setPassword("root");
	//獲得連接對(duì)象
	Connection connection = dataSource.getConnection();
	System.out.println(connection);
}

②創(chuàng)建Druid連接池 ????????

@Test
public void testDruid() throws Exception {
    //創(chuàng)建數(shù)據(jù)源
    DruidDataSource dataSource = new DruidDataSource();
    //設(shè)置數(shù)據(jù)庫連接參數(shù)
    dataSource.setDriverClassName("com.mysql.jdbc.Driver"); 
    dataSource.setUrl("jdbc:mysql://localhost:3306/test");   
    dataSource.setUsername("root");
    dataSource.setPassword("root");
    //獲得連接對(duì)象
    Connection connection = dataSource.getConnection();    
    System.out.println(connection);
}

③提取jdbc.properties配置文件

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=root

④讀取jdbc.properties配置文件創(chuàng)建連接池

@Test
public void testC3P0ByProperties() throws Exception {
    //加載類路徑下的jdbc.properties
    ResourceBundle rb = ResourceBundle.getBundle("jdbc");
    ComboPooledDataSource dataSource = new ComboPooledDataSource(); 
    dataSource.setDriverClass(rb.getString("jdbc.driver"));   
    dataSource.setJdbcUrl(rb.getString("jdbc.url")); 
    dataSource.setUser(rb.getString("jdbc.username")); 
    dataSource.setPassword(rb.getString("jdbc.password"));
    Connection connection = dataSource.getConnection();   
    System.out.println(connection);
}

Spring配置數(shù)據(jù)源

可以將DataSource的創(chuàng)建權(quán)交由Spring容器去完成

DataSource有無參構(gòu)造方法,而Spring默認(rèn)就是通過無參構(gòu)造方法實(shí)例化對(duì)象的

DataSource要想使用需要通過set方法設(shè)置數(shù)據(jù)庫連接信息,而Spring可以通過set方法進(jìn)行字符串注入


    
    
    
    

?測(cè)試從容器當(dāng)中獲取數(shù)據(jù)源

ApplicationContext applicationContext = new 
           ClassPathXmlApplicationContext("applicationContext.xml");
               DataSource dataSource = (DataSource) 
applicationContext.getBean("dataSource");
Connection connection = dataSource.getConnection();
System.out.println(connection);

抽取jdbc配置文件

首先,需要引入context命名空間和約束路徑:

命名空間:xmlns:context="Index of /schema/context"

約束路徑:Index of /schema/context ? ????????????????????????http://www.springframework.org/schema/context/spring-context.xsd



    
    
    
    

知識(shí)要點(diǎn)

Spring容器加載properties文件


 

2. Spring注解開發(fā)

1 Spring原始注解

Spring原始注解主要是替代的配置

注解 說明
@Component 使用在類上用于實(shí)例化Bean
@Controller 使用在web層類上用于實(shí)例化Bean
@Service 使用在service層類上用于實(shí)例化Bean
@Repository 使用在dao層類上用于實(shí)例化Bean
@Autowired 使用在字段上用于根據(jù)類型依賴注入
@Qualifier 結(jié)合@Autowired一起使用用于根據(jù)名稱進(jìn)行依賴注入
@Resource 相當(dāng)于@Autowired+@Qualifier,按照名稱進(jìn)行注入
@Value 注入普通屬性
@Scope 標(biāo)注Bean的作用范圍
@PostConstruct 使用在方法上標(biāo)注該方法是Bean的初始化方法
@PreDestroy

使用在方法上標(biāo)注該方法是Bean的銷毀方法

注意:

使用注解進(jìn)行開發(fā)時(shí),需要在applicationContext.xml中配置組件掃描,作用是指定哪個(gè)包及其子包下的Bean需要進(jìn)行掃描以便識(shí)別使用注解配置的類、字段和方法。


使用@Compont或@Repository標(biāo)識(shí)UserDaoImpl需要Spring進(jìn)行實(shí)例化。

//@Component("userDao")
@Repository("userDao")
public class UserDaoImpl implements UserDao {
 ? ?@Override
 ? ?public void save() {
 ?      System.out.println("save running... ...");
 ?  }
}

使用@Compont或@Service標(biāo)識(shí)UserServiceImpl需要Spring進(jìn)行實(shí)例化

使用@Autowired或者@Autowired+@Qulifier或者@Resource進(jìn)行userDao的注入

//@Component("userService")
@Service("userService")
public class UserServiceImpl implements UserService {
 ? ?/*@Autowired
 ? ?@Qualifier("userDao")*/
 ? ?@Resource(name="userDao")
 ? ?private UserDao userDao;
 ? ?@Override
 ? ?public void save() { ? ? ? 
 ?   ?userDao.save();
 ?  }
}

使用@Value進(jìn)行字符串的注入

@Repository("userDao")
public class UserDaoImpl implements UserDao {
 ? ?@Value("注入普通數(shù)據(jù)")
 ? ?private String str;
 ? ?@Value("${jdbc.driver}")
 ? ?private String driver;
 ? ?@Override
 ? ?public void save() {
 ? ? ? ?System.out.println(str);
 ? ? ? ?System.out.println(driver);
 ? ? ? ?System.out.println("save running... ...");
 ?  }
}

使用@Scope標(biāo)注Bean的范圍

//@Scope("prototype")
@Scope("singleton")
public class UserDaoImpl implements UserDao {
 ? //此處省略代碼
}

使用@PostConstruct標(biāo)注初始化方法,使用@PreDestroy標(biāo)注銷毀方法

@PostConstruct
public void init(){
    System.out.println("初始化方法....");
}
@PreDestroy
public void destroy(){
    System.out.println("銷毀方法.....");
}

2 Spring新注解

注解 說明
@Configuration 用于指定當(dāng)前類是一個(gè) Spring 配置類,當(dāng)創(chuàng)建容器時(shí)會(huì)從該類上加載注解
@ComponentScan 用于指定 Spring 在初始化容器時(shí)要掃描的包。 作用和在 Spring 的 xml 配置文件中的 一樣
@Bean 使用在方法上,標(biāo)注將該方法的返回值存儲(chǔ)到 Spring 容器中
@PropertySource 用于加載.properties 文件中的配置
@Import 用于導(dǎo)入其他配置類

?

?

Spring整合Junit

①導(dǎo)入spring集成Junit的坐標(biāo)

②使用@Runwith注解替換原來的運(yùn)行期

③使用@ContextConfiguration指定配置文件或配置類

④使用@Autowired注入需要測(cè)試的對(duì)象

⑤創(chuàng)建測(cè)試方法進(jìn)行測(cè)試

原文鏈接:https://blog.csdn.net/m0_59619191/article/details/124397829

欄目分類
最近更新