Annotations value set spring implemented injection

EDITORIAL

A long time ago to write notes in the proper way cloud notes, ready to give it up, migrate notes to be here. Article may have many shortcomings, please understand, welcome Gangster comments.

As used herein, the thing

  1. ecplipse
  2. spring

1. text

Annotation is provided in the profile scan path
- here the spring configuration based spring4.0 packet header, base-package annotations scanning path represented annotations

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:tx="http://www.springframework.org/schema/tx" 
xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd 
">
<context:component-scan base-package="com.etc" />
</beans>

Java class defined in the acquired objects
- the way to achieve the same manner as in the xml configuration file

//1 读取applicationContext.xml文件,创建1个对象工厂
      BeanFactory fac = new ClassPathXmlApplicationContext("applicationContext.xml"); 

      //2 使用class类来拿出对象
      //IFly p1_2 = fac.getBean(Plane.class);

      //2 通过id从工厂中拿出对象,指定为IFly.class类型
      IFly p1 = fac.getBean("plane",IFly.class);

      //3 取得的对象可以正常使用
      p1.fly();

Bean's create notes

@Component	--通用的注解,用于注解一些中立的对象
@Controller    --控制层
@Service	--业务层(按业务划分)
@Repository ---dao层(数据库操作层,按数据表划分)

ID is not specified ** Example: **

@Component

Example given ID **: **

@Component(value="book") 
@Component("book")

@Value assigns them
an example:

    @Value("1213")
    private int sid;

@Scope object life cycle
Example:

@Scope("prototype")

Expressed bean life cycle:

  • singleton- default singleton (object is created only once).
  • prototype- prototype, ie use the new (single cases only lazy creation).
  • request- and request the same life cycle.
  • session- and ssesion same life cycle.

@Autowired by default automatically inject byType

  1. @Autowired can be written on property or setter method, write directly attribute assignment, written on assignment by the setter method setter methods.
  2. @Autowired default assembled by type, default claim dependent objects must be present if a null value to allow, can set its required property is false, such as: @Autowired (required = false)
  3. If we want to use the property name assembly can be use in conjunction with @Qualifier comment

@Resource by default automatically inject byName

  1. @Resource this annotation belongs J2EE, can be written on property or setter method, write directly attribute assignment, written on assignment by the setter method setter methods.
  2. The name attribute: resolves to the bean property name property type: bean resolves to attribute types
  3. Use the name attribute is used byName automatic injection strategy, find the name (id) match bean assembled from the context, can not find an exception is thrown
  4. Is used when using the type attribute byType automatic injection strategy, find a unique type of bean matching assembled from the context, you can not find or find more, will throw an exception
  5. If neither type attribute name is assigned, the automatic injection strategy case byName by using reflection. If there is no match, then the match is a backoff primitive type, if a match is automatically assembled;
  6. If you specify the name and type, from the Spring context to find a unique match of bean assembled, can not find an exception is thrown

Corresponding to the object class named @Qualifier

  1. @Qualifier mean those who pass through this label, which indicates that the implementation class is what we need,
  2. Add @Qualifier annotation, note that the parameter names @Qualifier is one of the names we defined annotation bean, which is the bean id.
  3. @Qualifier specified bean class

2. summary

A long time ago to write notes in the proper way cloud notes, ready to give it up, migrate notes to be here. There are ambiguities Comments welcome, I would see the reply. This article is over, what are deficiencies please feel free to correct me.

Published 38 original articles · won praise 17 · views 1237

Guess you like

Origin blog.csdn.net/nineya_com/article/details/103532995