抄袭来源:https://blog.csdn.net/weixin_39805338/article/details/80770472
1.@Configuration注解
该类等价 与XML中配置beans,相当于Ioc容器,它的某个方法头上如果注册了@Bean,就会作为这个Spring容器中的Bean,与xml中配置的bean意思一样。
@Configuration注解的类必需使用
@Configurationpublic class MainConfig {//在properties文件里配置@Value(“${wx_appid}”)public String appid;protected MainConfig(){}@Beanpublic WxMpService wxMpService() {WxMpService wxMpService = new WxMpServiceImpl();wxMpService.setWxMpConfigStorage(wxMpConfigStorage());return wxMpService;}}
3.
@Controller,
@Service,
@Repository,
@Component
目前4种注解意思是一样,并没有什么区别,区别只是名字不同。使用方法:
1.使用
2. 在类上写注解:
@Controllerpublic class TestController {}
4. @PostConstruct 和 @PreDestory
实现初始化和销毁bean之前进行的操作,只能有一个方法可以用此注释进行注释,方法不能有参数,返回值必需是void,方法需要是非静态的。
例如:
public class TestService {@PostConstructpublic void init(){System.out.println("初始化");}@PreDestroypublic void dostory(){System.out.println("销毁");}}
@PostConstruct:在构造方法和init方法(如果有的话)之间得到调用,且只会执行一次。
@PreDestory:注解的方法在destory()方法调用后得到执行。
5. @Primary
自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常。
例如:
@Componentpublic class Apple implements Fruit{@Overridepublic String hello() {return "我是苹果";}}@Component@Primarypublic class Pear implements Fruit{@Overridepublic String hello(String lyrics) {return "梨子";}}public class FruitService {//Fruit有2个实例子类,因为梨子用@Primary,那么会使用Pear注入@Autowiredprivate Fruit fruit;public String hello(){return fruit.hello();}}
6. @Lazy(true)
用于指定该Bean是否取消预初始化,用于注解类,延迟初始化。
7. @Autowired
Autowired默认先按byType,如果发现找到多个bean,则,又按照byName方式比对,如果还有多个,则报出异常。
1.可以手动指定按byName方式注入,使用@Qualifier。
//通过此注解完成从spring配置文件中 查找满足Fruit的bean,然后按//@Qualifier指定pean
@Autowired
@Qualifier(“pean”)
public Fruit fruit;
2.如果要允许null 值,可以设置它的required属性为false,如:@Autowired(required=false) 
public Fruit fruit;
8. @Resource
默认按 byName自动注入,如果找不到再按byType找bean,如果还是找不到则抛异常,无论按byName还是byType如果找到多个,则抛异常。
可以手动指定bean,它有2个属性分别是name和type,使用name属性,则使用byName的自动注入,而使用type属性时则使用byType自动注入。
@Resource(name=”bean名字”)
或
@Resource(type=”bean的class”)
这个注解是属于J2EE的,减少了与spring的耦合。
 
9. @Async
java里使用线程用3种方法:<br />1. 继承Thread,重写run方法<br />2. 实现Runnable,重写run方法<br />3. 使用Callable和Future接口创建线程,并能得到返回值。<br />前2种简单,第3种方式特别提示一下,例子如下:
class MyCallable implements Callable<Integer> {private int i = 0;// 与run()方法不同的是,call()方法具有返回值@Overridepublic Integer call() {int sum = 0;for (; i < 100; i++) {System.out.println(Thread.currentThread().getName() + " " + i);sum += i;}return sum;}}
main方法:
public static void main(String[] args) {Callable<Integer> myCallable = new MyCallable(); // 创建MyCallable对象FutureTask<Integer> ft = new FutureTask<Integer>(myCallable); //使用FutureTask来包装MyCallable对象for (int i = 0; i < 100; i++) {System.out.println(Thread.currentThread().getName() + ” ” + i);if (i == 30) {Thread thread = new Thread(ft); //FutureTask对象作为Thread对象的target创建新的线程thread.start(); //线程进入到就绪状态}}System.out.println(“主线程for循环执行完毕..”);try {int sum = ft.get(); //取得新创建的新线程中的call()方法返回的结果System.out.println(“sum = ” + sum);} catch (InterruptedException e) {e.printStackTrace();} catch (ExecutionException e) {e.printStackTrace();}}
太多不想写了 参考文章url:https://blog.csdn.net/weixin_39805338/article/details/80770472
10.@Named
@Named和Spring的@Component功能相同。@Named可以有值,如果没有值生成的Bean名称默认和类名相同。比如
@Named public class Person 
或
@Named(“cc”) public class Person
11. @Inject
使用@Inject需要引用javax.inject.jar,它与Spring没有关系,是jsr330规范。
与@Autowired有互换性。
12. @Singleton
只要在类上加上这个注解,就可以实现一个单例类,不需要自己手动编写单例实现类
13.@Valid,@Valided
@Valid
   网上一大片使用@Valid失效不能用的情况。为什么呢?
1.@Valid必需使用在以@RequestBody接收参数的情况下。
2.使用ajax以POST方式提示数据,禁止用Fiddler以及浏览器直接访问的方式测试接口
3.用
4.@Valid是应用在javabean上的校验。
<dependency><groupId>org.hibernate</groupId><artifactId>hibernate-validator</artifactId><version>4.2.0.Final</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-annotations</artifactId><version>2.5.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId><version>2.5.3</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.5.3</version></dependency><dependency><groupId>org.codehaus.jackson</groupId><artifactId>jackson-mapper-asl</artifactId><version>1.9.8</version></dependency><dependency><groupId>com.fasterxml.jackson.module</groupId><artifactId>jackson-module-jaxb-annotations</artifactId><version>2.5.3</version>
https://blog.csdn.net/weixin_39805338/article/details/80770472
