Spring之在学习(4)

[图片上传失败...(image-c60c10-1512694478952)]

使用基于java类的配置信息

普通类

package com.smart.conf;

public class UserDao {

}

package com.smart.conf;

public class LogDao {

}

package com.smart.conf;

public class LogonService {

    private LogDao logDao;
    private UserDao userDao;
    public LogDao getLogDao() {
        return logDao;
    }
    public void setLogDao(LogDao logDao) {
        this.logDao = logDao;
    }
    public UserDao getUserDao() {
        return userDao;
    }
    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }
    
    public void printHelllo(){
        System.out.println("hello!");
    }
    
}

使用注解
package com.smart.conf;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConf {
    @Bean
    public UserDao userDao(){
       return new UserDao();    
    }
    
    @Bean
    public LogDao logDao(){
        return new LogDao();
    }
    
    @Bean
    public LogonService logonService(){
        LogonService logonService = new LogonService();
        logonService.setLogDao(logDao());
        logonService.setUserDao(userDao());
        return logonService;
    }
}

相当于XML


    
    

凯哥

package com.kaishengit;

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

@Configuration
@ComponentScan
@EnableAspectJAutoProxy
@PropertySource("classpath:config.properties")
@EnableTransactionManagement
public class Application {

    @Autowired
    private Environment environment;

    @Bean
    public BasicDataSource dataSource() {
        BasicDataSource basicDataSource = new BasicDataSource();
        basicDataSource.setDriverClassName(environment.getProperty("jdbc.driver"));
        basicDataSource.setUrl(environment.getProperty("jdbc.url"));
        basicDataSource.setUsername(environment.getProperty("jdbc.username"));
        basicDataSource.setPassword(environment.getProperty("jdbc.password"));
        return basicDataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate(DataSource dataSource) {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource);
        return jdbcTemplate;
    }

    @Bean
    public NamedParameterJdbcTemplate namedParameterJdbcTemplate(DataSource dataSource) {
        return new NamedParameterJdbcTemplate(dataSource);
    }

    @Bean
    public DataSourceTransactionManager dataSourceTransactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}

SpringBoot测试

普通类
package com.toltech.config;

/**
 * Created by wanggs on 2017/8/9.
 */
public class Dog {
    public void eat(){
        System.out.println("%%%%%%%%%%%%%%%%%%%");
    }
}

注解
/**
 * Created by wanggs on 2017/8/9.
 */
@Configuration
public class Animal {
    @Bean
    public Dog dog(){
        return new Dog();
    }
}

测试
package com.toltech;

import com.toltech.config.Animal;
import com.toltech.config.Dog;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
@ComponentScan("com.toltech.config")
public class ApplicationTests {
    @Autowired
    Animal animal;

    @Autowired
    Dog dog;
    @Test
    public void contextLoads() {
        animal.dog().eat();
    }
    @Test
    public void dog(){
        dog.eat();
    }

}

spring事务

Spring之在学习(4)_第1张图片
spring事务.png
@Configuration
public class RestConfig {

    //自动从注册中心发现服务,并采用轮询的方式进行负载均衡调用
    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}

你可能感兴趣的:(Spring之在学习(4))