其他分享
首页 > 其他分享> > @Configurable

@Configurable

作者:互联网

@Configurable


引用原文


内容如下
Not, all types(beans) used in a Spring Based Application are Spring managed. Sometimes, we need to inject some dependency into some Types which are not Spring Managed. The most common example being, injection of Service or DAO layer within the Entity class. Now the instances of Entity classes are being created using the new operator, i.e. they are not Spring Managed. Now we want to inject the corresponding Service or DAO Type which is Spring Managed within these Entity Types, and these can be performed with the help of @Configurable. It is the Aspects, which performs all the underlying engineering and ,makes this thing possible. Let’s see an example.

不是基于Spring的应用程序中使用的所有类型(bean)都是Spring管理的。有时,我们需要将一些依赖注入到一些非Spring管理的类型中。最常见的例子是在实体类中注入服务或DAO层。现在实体类的实例使用new创建,也即它们不是被Spring管理的。现在我们想要在这个类型中注入对应的Service或DAO类型,被注入类型是被Spring管理的,以上这些操作可以在 @configure 的帮助下完成。它是执行所有基础工程,并使之成为可能的因素之一。让我们看一个例子。


@Configurable
public class Person {

 @Autowired
 ResourceBundleThemeSource themeSource;
 
 private String firstName;
 private String lastName;
 private int age;
 private Address address;
 @CustomDateFormat
 Date birthDate;
 MultipartFile profile;
 
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public Address getAddress() {
  return address;
 }
 public void setAddress(Address address) {
  this.address = address;
 }
 public Date getBirthDate() {
  return birthDate;
 }
 public void setBirthDate(Date birthDate) {
  this.birthDate = birthDate;
 }
 public MultipartFile getProfile() {
  return profile;
 }
 public void setProfile(MultipartFile profile) {
  this.profile = profile;
 }
 
 @Override
 public String toString() {
  return "Person [ThemeSource=" + themeSource.getTheme("default")
    + ", firstName=" + firstName + ", lastName=" + lastName
    + ", age=" + age + ", address=" + address + ", birthDate="
    + birthDate + ", profile=" + profile + "]";
 }
}

Here, Person can be considered as a Model or Entity class, and it has got a bean of type ResourceBundleThemeSource injected into it. This bean is Spring Managed and the Person class is annotated with @Configurable. I have defined the themeSource in a java based configuration, instead of XML based as:

在这里,Person可以被看作是一个模型或实体类,它有一个类型为ResourceBundleThemeSource的bean注入其中。这个bean是被Spring管理的,Person类是用 @Configure 注释的。我在java的配置中定义了themeSource,而不是在XML中配置的:


@Configuration
@Import(........
@EnableSpringConfigured
public abstract class SpringWebApplicationConfig extends WebMvcConfigurerAdapter {
@Autowired
Environment environment;

@Value(.......
 
@Bean
public ResourceBundleThemeSource themeSource()
  {
   ResourceBundleThemeSource resourceBundleThemeSource = new ResourceBundleThemeSource();
   resourceBundleThemeSource.setBasenamePrefix("theme-");
   return resourceBundleThemeSource;
  }

}

One of the most important things to be noticed out here, is that the class is annotated with @EnableSpringConfigured. The following dependencies should be included in the pom.xml along with the plugin definitions.

这里需要注意的最重要的事情之一是,该类使用@EnableSpringConfigured进行注释。xml中应该包含以下依赖项以及插件。

<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-aspects</artifactId>
	<version>4.1.6.RELEASE</version>
</dependency>

<dependency>
	<groupId>javax.persistence</groupId>
	<artifactId>persistence-api</artifactId>
	<version>1.0.2</version>
</dependency>

<dependency>
	<groupId>javax.cache</groupId>
	<artifactId>cache-api</artifactId>
	<version>1.0.0</version>
</dependency>

<plugin>
	<groupId>org.codehaus.mojo</groupId>
	<artifactId>aspectj-maven-plugin</artifactId>
	<version>1.6</version>
	
	<configuration>
		<complianceLevel>1.6</complianceLevel>
		<source>1.6</source>
		<target>1.6</target>
		<outxml>true</outxml>
		<verbose>true</verbose>
		<showWeaveInfo>true</showWeaveInfo>
		<aspectLibraries>
			<aspectLibrary>
				<groupId>org.springframework</groupId>
				<artifactId>spring-aspects</artifactId>
			</aspectLibrary>
		</aspectLibraries>
	</configuration>
	
	<executions>
		<execution>
			<phase>process-classes</phase>
			<goals>
				<goal>compile</goal>
			</goals>
		</execution>
	</executions>
</plugin>

Now after maven build, the target model class Person, would get the required dependencies, weaved in it, by AspectJ. So, every time we create an object of Person class which necessarily is not Spring-Managed will have the declared dependency weaved in it. We can verify it just by logging the object toString() versione as I have done in one of my Controller method just like the following.

现在,在maven构建之后,目标模型类Person将通过AspectJ获得所需的依赖项。因此,每当我们创建Person类的对象时(它不一定是spring管理的),声明的依赖关系就会交织在其中。我们可以通过记录对象toString()版本来验证它,就像我在控制器方法中所做的那样,如下所示:


@RequestMapping(value="Emp.......
public ModelAndView getEmployeeProfileLayout()
 {
  ...............
  Person person = new Person();
  person.setLastName("PAUL");
  log.debug(" The Person is:"+person);
 
  ............
  ...................
 }

We can now find that the person object would contain an instance of type ResourceBundleThemeSource even though the object has been created using new operator.

现在我们可以发现person对象将包含ResourceBundleThemeSource类型的实例,即使该Person是使用new创建的。

标签:return,Spring,class,ResourceBundleThemeSource,Configurable,Person,public
来源: https://blog.csdn.net/qq_42786993/article/details/88045834