一.复杂属性注入
1.1 这里的复杂属性包括:Array,Set,List,Map,Properties等
1.2 注入操作
实体类:person.java
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Person {
private String[] names;
private Set hobby;
private List subjects;
private Map phones;
private Properties friends;
}
1.3 applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="person" class="com.gh.entity.Person">
<property name="names">
<array>
<value>张三</value>
<value>狗蛋</value>
</array>
</property>
<property name="hobby">
<set>
<value>编程</value>
<value>游戏</value>
</set>
</property>
<property name="subjects">
<list>
<value>java</value>
<value>数据结构</value>
</list>
</property>
<property name="friends">
<props>
<prop key="fri1">李四</prop>
<prop key="fri2">王五</prop>
<prop key="fri3">赵六</prop>
</props>
</property>
<property name="phones">
<map>
<entry key="中国移动" value="10086"></entry>
<entry key="消费者协会" value="12315"></entry>
</map>
</property>
</bean>
</beans>
1.4 测试:获取属性值
@Test
public void getPerson(){
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
Person person = ioc.getBean("person", Person.class);
System.out.println(person);
}
二.获取连接对象
2.1 创建dao层接口及实现类
import com.gh.entity.User;
public interface UserDao {
User findPersonById(Integer id);
}
import com.alibaba.druid.pool.DruidDataSource;
import com.gh.dao.UserDao;
import com.gh.entity.User;
public class UserDaoImpl implements UserDao {
private DruidDataSource dataSource;
public DruidDataSource getDataSource() {
return dataSource;
}
public void setDataSource(DruidDataSource dataSource) {
this.dataSource = dataSource;
}
public User findPersonById(Integer id) {
return null;
}
}
2.2 创建applicationContext.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 https://www.springframework.org/schema/context/spring-context.xsd">
<!--加载properties配置文件-->
<context:property-placeholder location="classpath*:jdbc.properties"></context:property-placeholder>
<bean id="userDao" class="com.gh.dao.impl.UserDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>
2.3 测试能否获取到连接对象
// 测试获取德鲁伊连接对象
@Test
public void TestConnection() throws SQLException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext02.xml");
UserDaoImpl userDao = ioc.getBean("userDao", UserDaoImpl.class);
System.out.println(userDao.getDataSource().getConnection());
}
@Test
public void TestConnection02() throws SQLException {
ClassPathXmlApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext02.xml");
DruidDataSource dataSource = ioc.getBean("dataSource", DruidDataSource.class);
System.out.println(dataSource.getConnection());
}
三.注解开发
3.1 概述:把注入bean的方式由原来在配置文件中书写改为给类、属性、方法添加注解的方式
- @Component——可以给所有类添加
- @Repository——可以给持久层添加
- @Service——可以给业务层添加
- @Controller——可以给控制层添加
- @Bean——给方法添加,将返回结果注入
- @Scope(“singleton”)——设置对象创建模式
//@Component
@Repository
public class UserDaoImpl implements UserDao {
@Bean
public DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/homework?useSSL=false&serverTimezone=GMT&characterEncoding=UTF8&useUnicode=true");
dataSource.setUsername("root");
dataSource.setPassword("root");
return dataSource;
}
@Override
public Integer insertUser(User user) {
System.out.println("UserDaoImpl...insertUser...");
return null;
}
}
3.2 简单注解赋值
@values——可以给基本类型,String ,Date进行赋值,如
public class User {
@Value("1")
private Integer id;
@Value("张三")
private String username;
@Value("sanzhang")
private String password;
}
3.3 对象注解——给类中被引用的对象寻找注入的数据
3.4 生命周期相关注解
- @PostConstruct——初始化注解
- @PreDestory——注销方法注解
@PostConstruct
public void init(){
System.out.println("User...init");
}
@PreDestroy
public void destroy(){
System.out.println("User...destroy");
}
3.5 配置相关注解
- @Configuration——把此类标记为配置
- @ComponentScans—— 扫描文件
- @Import—— 导入其他的配置文件
- @PropertySource—— 导入配置文件