java注释配置文件_Spring 使用注解和Java配置文件

九、使用注解开发

配置文件:

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

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

https://www.springframework.org/schema/context/spring-context.xsd">

一.使用注解实现自动装配

1.注解:Autowired

可以直接在setter或着属性上使用;

@Autowired(required="true")//默认是true,是否可以为空值

@Qualifier(value = "address2")

使用这个注解的话,可以不用写setter方法;它会在bena中寻找符合命名规范的byTypee属性;如果命名找到的不止一个,它就会去找寻id一样的;如果类名也找不到,你又想自动装配的话,就可以使用@Qualifier 默认的配置自动装配的名字,通过id

2.注解:@Resource()

resource里面有两个属性,type和name;就是byname和bytype的区别;默认的是使用byName,找不到再用byType

3.注解:@Nullable

字段被这个标记的话,就可以为空,且不会报错;

二.使用注解开发

配置文件:

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

https://www.springframework.org/schema/beans/spring-beans.xsd

http://www.springframework.org/schema/context

https://www.springframework.org/schema/context/spring-context.xsd">

使用注解开发的第一步就是导入支持;

//扫描包里面的所有注解

//支持注解开发

1.@Component

就是把我们的类放到spring中进行一个托管;相当于下面的语句:

它还可以根据类在不同的层次中位置进行一个划分,但是实际作用一样;

@Repository Dao层

@Service service层

@Controller servlet层

2.@Value ("value")

给对象属性赋值;

@Controller

public class User {

@Value ("saxon1")

public String name;

}

3.自动装配

在上面的笔记上有;

4.bean作用域

就是第七节讲的那个,只不过现在在类上配置;

@Controller

@Scope("singleton"||"prototype")

public class User {

@Value ("saxon1")

public String name;

}

5.小结

xml:万能,什么都可以设置,维护简单,快捷;

注解:简单,但是不是自己的类,别人引用不了例如:ref

所以:注解一般用来赋值,xml一般用来管理bean

十、使用Java配置文件

1.@Configuration

使用注解将Java类变成一个注解文件

package com.saxon.config;

import com.saxon.Dao.User;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class applicationContext {

@Bean

public User user(){

return new User ();

}

}

接下来看一下@Configuration里面的东西:

//

// Source code recreated from a .class file by IntelliJ IDEA

// (powered by FernFlower decompiler)

//

package org.springframework.context.annotation;

import java.lang.annotation.Documented;

import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;

import org.springframework.core.annotation.AliasFor;

import org.springframework.stereotype.Component;

@Target({ElementType.TYPE})

@Retention(RetentionPolicy.RUNTIME)

@Documented

@Component

public @interface Configuration {

@AliasFor(

annotation = Component.class

)

String value() default "";

boolean proxyBeanMethods() default true;

}

从我现在的知识储备来看就是一个组件,就是我们上一次讲到的Component;实际上我们自己写的类变成一个组件来配置文件;

2.@Import

就是我们以前说的,把各个部分的xml文件合成一个,可以通过一个主要的xml访问所有的bean;这里也是同样的作用;

3.@ComponentScan

@ComponentScan("com.saxon.config")

扫描指定的包下的所有注解,与前面的功能一致;

4.@Bean

使用这个注解,将一个类交给我们的spring托管,相当于xml配置文件的Bean标签;

@Bean

public User user(){

return new User ();

}

函数名就是我们的bean标签的id,返回值就是对象 就是class标签;

还有一个问题有待解决,就是@Bean与@Component都是把类交给我们的spring托管,那么他们的区别又是什么呢;欢迎知道的帮我解决一下

自学总结

学习地址:狂神说Java

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值