spring 相关注解

来源:互联网 发布:淘客采集上传淘宝店铺 编辑:程序博客网 时间:2024/06/01 09:33
注解说明

@PropertySource:属性源,key-value属性对抽象,比如用于配置数据
@PropertySource(ignoreResourceNotFound=true, name="applicationConfiguration", value={"classpath:configuration.properties"})
ignoreResourceNotFound:忽视资源找不到, 允许忽略不存在的配置文件
name:key值
value:匹配文件
@PropertySources在Spring 4版本中,Spring提供了一个新的注解—— @PropertySources ,是为多配置文件而准备的。

@PropertySources({

@PropertySource("classpath:config.properties"),

@PropertySource("classpath:db.properties")

})

@ImportResource: 导入xml配置项

         @ImportResource("classpath:dubbo-customer.xml")  ,在maven项目中,自动读取resource文件夹下的dubbo-customer.xml文件

@Import: 导入CDConfig的配置

       @Import(CDConfig.class):导入CDConfig的配置

@Configuration: 可理解为用spring的时候xml里面的<beans>标签,多用于配置类


@Bean: 可理解为用spring的时候xml里面的<bean>标签,一个configuation可以有多个bean

@Controller标注一个控制器组件类

 • 例如

@Controller

 public class SoftCreateController extends SimpleBaseController {}

 • 或者

@Controller("userController")


@Service标注一个业务逻辑组件类

• 例如

  @Service

 public class SoftCreateServiceImpl implements ISoftCreateService {}

 • 或者

 @Service("softCreateServiceImpl")

@Autowired:根据bean 类型从spring 上线文中进行查找,注册类型必须唯一

• 例如

 @Autowired

private ISoftPMService softPMService;

• 或者

@Autowired(required=false)

@RequestMapping:用来定义类访问的URL,或者为每个方法指定一个

把@RequestMapping放在类级别上,这可令它与方法级别上的

@RequestMapping注解协同工作,取得缩小选择范围的效果。 

例如: 

@RequestMapping("/test")   //该类下的所有访问路径都在/test之下。 

public class TestController {} 

将@RequestMapping用于整个类不是必须的,如果没有配置,所有的方法的访问路径配置将是完全独立的,没有任何关联。 

完整的参数项为:@RequestMapping(value="",method = {"",""},headers={},params={"",""}),各参数说明如下:

value :String[] 设置访问地址 

method: RequestMethod[]设置访问方式,字符数组,查看RequestMethod类,包括GET, HEAD, POST, PUT, DELETE, OPTIONS, TRACE,常用RequestMethod.GET,RequestMethod.POST 

headers:String[] headers一般结合method = RequestMethod.POST使用 

params: String[] 访问参数设置,字符数组 例如:userId=id 


@RequestParam

@RequestParam("id")

http://localhost/itxxzSpring4?method=listBoardTopic&id=1&userId=10&userName=tomlistBoardTopic(@RequestParam("id")int topicId,User user) 中的 topicId 绑定到 id 这个 URL 参数, 那么可以通过对入参使用 @RequestParam 注解来达到目的 

@RequestParam(required=false):参数不是必须的,默认为true

@RequestParam(value="id",required=false)


@ModelAttribute

    1、@ModelAttribute放在方法的注解上时,代表的是:该Controller的所有方法在调用前,先执行此@ModelAttribute方法

   @ModelAttribute

    public void preRun() {

        System.out.println("Test Pre-Run");

    }

    2、可以做为Model输出到View时使用

@Controller

@RequestMapping(value="attr")

public class TestModelAttributeController {

    private static List<Account> accounts = new ArrayList<Account>();

    {

        accounts.add(new Account());

        accounts.add(new Account());

        Account ac1 = accounts.get(0);

        Account ac2 = accounts.get(1);

        ac1.setUserName("Robin");

        ac1.setPassword("123123");

        ac2.setUserName("Lucy");

        ac2.setPassword("123456");

    }


    @RequestMapping(method=RequestMethod.GET)

    public String index() {

        System.out.println("index");

        return "TestModelAttribute/index";

    }

    @ModelAttribute("accounts")

    public List<Account> getAccounts() {

        System.out.println("getAccounts");

        return accounts;

    }

}


@Resource:默认按bean 的name 进行查找,如果没有找到会按type 进行查找,与@Autowired 类 似

private DataSource dataSource; // inject the bean named 'dataSource'

 • 或者

@Resource(name="dataSource")

@Resource(type=DataSource.class)

@PostConstruct在方法上加上注解@PostConstruct ,这个方法就会在Bean 初始化之后被Spring 容器执 行

(注:Bean 初始化包括,实例化Bean ,并装配Bean 的属性(依赖注入))。

@PreDestroy:在方法上加上注解@PreDestroy ,这个方法就会在Bean 被销毁前被Spring 容器执行。


@Repository标注一个DAO组件类,与@Controller 、@Service 类似,都是向spring 上下文中注册bean

@Component (不推荐使用):是所有受Spring 管理组件的通用形式,Spring 还提供了更加细化的注解形式:  @Repository 、@Service 、@Controller ,它们分别对应存储层Bean ,业务层Bean ,和展示层Bean 


@Scope:在使用XML 定义Bean 时,可以通过bean 的scope 属性来定义一个Bean 的作用范围,同样可以通过@Scope 注解来完成

       @Scope中可以指定如下值:

       singleton:定义bean的范围为每个spring容器一个实例(默认值)

       prototype:定义bean可以被多次实例化(使用一次就创建一次)

       request:定义bean的范围是http请求(springMVC中有效)

       session:定义bean的范围是http会话(springMVC中有效)

       global-session:定义bean的范围是全局http会话(portlet中有效) 

@Scope("session")
@Repository()
public class UserSessionBean implementsSerializable {} 


@SessionAttributes:Spring 允许我们有选择地指定 ModelMap 中的哪些属性需要转存到 session 中,以便下一个请求属对应的 ModelMap 的属性列表中还能访问到这些属性。这一功能是通过类定义处标注 @SessionAttributes 注解来实现的。

@SessionAttributes 只能声明在类上,而不能声明在方法上。

• 例如

@SessionAttributes("currUser") // 将ModelMap 中属性名为currUser 的属性

@SessionAttributes({"attr1","attr2"})

@SessionAttributes(types = User.class)

@SessionAttributes(types = {User.class,Dept.class})

@SessionAttributes(types = {User.class,Dept.class},value={"attr1","attr2"})



@Required负责检查一个bean在初始化时其声明的 set方法是否被执行, 当某个被标注了 @Required 的 Setter 方法没有被调用,则 Spring 在解析的时候会抛出异常,以提醒开发者对相应属性进行设置。 @Required 注解只能标注在 Setter 方法之上。因为依赖注入的本质是检查 Setter 方法是否被调用了,而不是真的去检查属性是否赋值了以及赋了什么样的值。如果将该注解标注在非 setXxxx() 类型的方法则被忽略

@required             

public  setName(String name){}



@Qualifier:使用@Autowired 时,如果找到多个同一类型的bean,则会抛异常,此时可以使用@Qualifier("beanName"),明确指定bean的名称进行注入,此时与 @Resource指定name属性作用相同

@Autowired

 @Qualifier("softService")

private ISoftPMService softPMService;


日常更新,下一步先自己搭起框架,以便学习并了解spring mvc同springboot。








原创粉丝点击