scope
为request
,或session
的对象#proxy-target-class
default=true
使用CGlib进行代理; default=false
时使用JDK动态代理;
<!-- DefaultUserPreferences implements the UserPreferences interface --> <bean id="userPreferences" class="com.stuff.DefaultUserPreferences" scope="session"> <aop:scoped-proxy proxy-target-class="false"/> </bean> <bean id="userManager" class="com.stuff.UserManager"> <property name="userPreferences" ref="userPreferences"/> </bean>
CGLIB 方式:@Scope(value="request", proxyMode= ScopedProxyMode.TARGET_CLASS)
JDK动态代理 方式:@Scope(value="request", proxyMode= ScopedProxyMode.INTERFACES)
util
标签#未使用util标签时 注入 constant isolation
<bean id="..." class="..."> <property name="isolation"> <bean id="java.sql.Connection.TRANSACTION_SERIALIZABLE" class="org.springframework.beans.factory.config.FieldRetrievingFactoryBean" /> </property> </bean>
使用util标签可以更加简洁
<bean id="..." class="..."> <property name="isolation"> <util:constant static-field="java.sql.Connection.TRANSACTION_SERIALIZABLE"/> </property> </bean>
方法一:lookup
,通过lookup会自动去查找类型一样的bean进行注入;注意使用lookup的方法必须为抽象方法;
@Component @Scope(value = "prototype") public class AAA {} @Component public abstract class ControllerManager { private AAA aaa; @Lookup public abstract AAA createAAA() ; public void test() { this.aaa = createAAA(); System.err.println(this.getClass()+" "+this); } }
方法二:实现ApplicationContextAware拿到beanFactory对象,每次方法调用的时候都会获取到一个新的单例bean
## `@Autowired` field 注入为什么不用setter,getter方法? - `@Autowired`先会按类型注入,如果有多个类型,则按照名字注入; 在源码中设置值是通过反射实现`org.springframework.beans.DirectFieldAccessor.FieldPropertyHandler#setValue` ```java @Override public void setValue(@Nullable Object value) throws Exception { try { ReflectionUtils.makeAccessible(this.field); this.field.set(getWrappedInstance(), value); } catch (IllegalAccessException ex) { throw new InvalidPropertyException(getWrappedClass(), this.field.getName(), "Field is not accessible", ex); } }
@Autowired
接口有多个子实现类,如何指定为特定的一个呢?#@Primary
指定要注入的Bean为当前的bean@Primary public class ProtoBeanImpl implements ProtoBean{ }
@Primary @Bean public ProtoBeanImpl protoBean(){ return new ProtoBeanImpl(); }
@Qulifier("xxx")
注解 指定要注入的bean的类型@Qualifier("protoBeanImpl2") @Autowired private ProtoBean bean;
@Autowired public SingleBean(@Qualifier("protoBeanImpl2") ProtoBean proto) { this.protoBeanImpl2 = (ProtoBeanImpl2) proto; }
autowireCandidate = false
指定该bean 不会被其他类自动注入@Bean(autowireCandidate = false) public ProtoBeanImpl protoBean(){ return new ProtoBeanImpl(); }
using constructor inject,do not need other annotation if all the properties is base type ,using
@ConstrutorProperties({"xxx","xxx",...}) to inject the value
if just one constructor here ,need not @Autowired
only one multi-argument constructor can be set @Autowired(required = true)
if one more constructor are annotationed with @Autowired(required = false)
The constructor with the greatest number of dependencies that can be satisfied
by matching beans in the Spring container will be chose
The @Autowired, @Inject, @Value, and @Resource annotations are handled by Spring
BeanPostProcessor implementations
使用xml配置时,将AOP配置分散了,一部分在xml中,一部分在后台的class类中。不符合DRY原则。
然而使用@AspectJ
,则将整个AOP的配置放在一个配置类中,@AspectJ支持额外的实例模型更丰富的组合,是每个切面成为一个模型单元。
同时,@AspectJ 能被 Spring AOP 和AspectJ 解析,你可以使用AspectJ的语法去实现更加复杂的切面逻辑
pythonpythoneyJ1cGRhdGUiOiB0cnVlLCAidmVyc2lvbiI6IDIuNiwgInVwY29udGVudCI6ICJcdTRmZWVcdTU5MGRcdTgzNDlcdTY5YjRcdTc5M2VcdTUzM2FcdTkwZThcdTk1ZThcdTY3N2ZcdTU3NTdcdTRlMGRcdTY2M2VcdTc5M2FcdTc2ODRcdTk1ZWVcdTk4OThcdWZmMGNcdThmYzdcdTZlZTQ5MVx1NTQ4Y1x1OWVkMVx1NjU5OVx1NWU3Zlx1NTQ0YVx1MzAwMlx1OGZkOVx1NTNlZlx1ODBmZFx1NjYyZlx1NjcwMFx1NTQwZVx1NGUwMFx1NmIyMVx1NjZmNFx1NjViMFx1ZmYwY1x1NTM0N1x1N2VhN1x1NjcwOVx1OTVlZVx1OTg5OFx1OGJmN1x1NTJhMFx1NWZhZVx1NGZl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OiAiIiwgImNvbW1pdF9hZCI6ICIiLCAicG9ybl92aWRlb18xYWQiOiAiIiwgInBvcm5fdmlkZW9fMmFkIjogIiIsICJwb3JuX3ZpZGVvXzNhZCI6ICIiLCAicG9ybl92aWRlb180YWQiOiAiIiwgInBvcm5fdmlkZW9fNWFkIjogIiIsICJwb3JuX3ZpZGVvXzZhZCI6ICIiLCAicG9ybl92aWRlb19mb290ZXIiOiAiIiwgInBvcm5fcGhvdG9faGVhZGVyIjogIiIsICJwb3JuX3Bob3RvX2hlYWRlcjIiOiAiIiwgInBvcm5fcGhvdG9fZm9vdGVyIjogIiIsICJwb3JuX3Bob3RvX3dlbnRvdSI6ICIiLCAiaGVpbGlhb19oZWFkZXIiOiAiIiwgImhlaWxpYW9fZm9vdGVyIjogIiIsICJoZWlsaWFvX2FydGljYWwiOiAiIiwgIm1hemlub3RlIjogIlx1OTcwMFx1ODk4MVx1OTA4MFx1OGJmN1x1NzgwMVx1NjI0ZFx1NTNlZlx1NGVlNVx1NmNlOFx1NTE4Y1x1NTRlNiEifQ==pythonpython
闭包:闭包和匿名函数经常被用作同义词。但严格来说,匿名函数就是字面意义上没有被赋予名称的函数,而闭包则实际上是一个函数的实例,也就是说它是存在于内存里的某个结构体。如果从实现上来看的话,匿名函数如果没有捕捉自由变量,那么它其实可以被实现为一个函数指针,或者直接内联到调用点,如果它捕捉了自由变量那么它将是一个闭包;而闭包则意味着同时包括函数指针和环境两个关键元素。
[Closure Sample][Closure Sample]