ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

spring笔记(中)

2021-07-07 19:01:53  阅读:157  来源: 互联网

标签:笔记 spring private dataSource DruidDataSource import public User


一.复杂属性注入

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—— 导入配置文件

标签:笔记,spring,private,dataSource,DruidDataSource,import,public,User
来源: https://blog.csdn.net/qq_46083662/article/details/118545319

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有