spring boot 学习笔记(1)

 @ComponentScan@EntityScan 和@SpringBootApplication ,我们通常用它来注释启动类。spring boot的启动类也就是main类一般都放在根文件夹下。比如:

    图片传不上来~你知道吗CSDN?

主程序如下:

package testRedis.main;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

/**
 * Hello world!
 */
@EnableAutoConfiguration
@ComponentScan
public class App 
{
    public static void main( String[] args )
    {
	SpringApplication app =new SpringApplication(App.class);
        app.run(args);
    }
}
 

上个会启动spring boot的主程序,初始化bean。这个bean可以是:

	@Bean
		public JedisConnectionFactory jedisConnectionFactory(@Qualifier("jedisPoolConfig")JedisPoolConfig jedisPoolConfig){
			JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
			jedisConnectionFactory.setHostName(host);
			jedisConnectionFactory.setPort(port);
			jedisConnectionFactory.setTimeout(timeout);
			jedisConnectionFactory.setPoolConfig(jedisPoolConfig);
			return jedisConnectionFactory;
		}
		

也就是说它在启动完成对这些bean的“装载”。

 @EnableAutoConfiguration 注释默认当前文件夹为搜索的根文件夹。如果你用不带参数的 @ComponentScan 注释在主程序中,它会自动装载@Component@Service@Repository@Controller,做为“spring bean” 。

具体一个例子,我在测试redis时,启动spring boot 需要 执行mutiThread方法,需要默认启动它。因此我在方法前加上@Bean,并且在类前方加上了@configuration注释。这样 @ComponentScan 就能找到@configuration里下配置的@bean,尝试装载bean的同时执行了mutiThread方法。当然这只是为了学习才这样做。去掉 @ComponentScan 和@configuration中的任意一个,mutiThread都不能执行。

@Configuration
@EnableConfigurationProperties
public class testCase1 {
	@Autowired
	@Qualifier("taskExcute")
	private TaskExcute taskExcute;
	@Value("${cl.size:1000000}")
	private int size;
	
    @Bean
    public String mutiThread(){
    	Map<String,Object> map=new HashMap<String,Object>();
		for(int i=size;i>0;i--){
			map.put("name"+i, "foo");
		}
    	try {
			taskExcute.dotasks(map);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	return "hello world!";
    }
}



 @ComponentScan 就不需要指定 basePackage 。如果你的主main程序在根文件夹的话@SpringBootApplication 也可以起同样的效果。

spring boot会基于你的class path 下的jar包依赖自动配置你的spring程序。具体什么情况,有待深入研究。


这个功能自动配置加载的功能,可以通过以下方式禁掉。

import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

如果这个类没有在 classpath, 你可以用 excludeName 这个注释属性。

jar运行方式。

你可以借助maven或者gradle打包的你程序,生成可执行jar。我用的maven,执行clean install命令,可以直接install如果是第一次运行,或者用package。

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值