spring in a bean property assignment
- xml file properties tab settings
<bean id="student" class="com.enjoy.study.cap10.Student" > <property name="id" value="18"/> <property name="name" value="wxf"/> </bean>
- annotation
- @Autowired
- @Value
- @Resource
This chapter focuses on the use of blog comment assignment
@Autowired
@Value
@Configuration public class CapMainConfig { @Bean public Student student(){ return new Student(); } }
public class TestCap { @Test public void testMethod(){ ApplicationContext context = new AnnotationConfigApplicationContext(CapMainConfig.class); Student student = (Student) context.getBean("student"); System.out.println("student = " + student); } }
Character form @Value ( "")
public class Student { @Value("12") private Integer id; @Value("wxf") private String name; @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + '}'; } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
result
student = Student{id=12, name='wxf'}
Expressions @Value ( "$ {}"), @ Value ( "# {}")
By @Value ( "$ {}") can obtain the corresponding attribute value defined in the file attribute
@Value ( "# {}") represents SpEl is generally used for obtaining the bean property, or call a method of the bean