我不知道如何以編程方式創建bean。 這是其在.xml配置如何製造:如何在Spring Boot中以編程方式創建@Bean
<bean id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="mail.mycompany.com"/>
</bean>
<!-- this is a template message that we can pre-load with default state -->
<bean id="templateMessage"
class="org.springframework.mail.SimpleMailMessage">
<property name="from" value="[email protected]"/>
<property name="subject" value="Your order"/>
</bean>
<bean id="orderManager"
class="com.mycompany.businessapp.support.SimpleOrderManager">
<property name="mailSender" ref="mailSender"/>
<property name="templateMessage" ref="templateMessage"/>
</bean>
我知道,它必須是這樣的,但我不知道如何去完成它:
@Configuration
public class MailSender {
@Bean
public JavaMailSenderImpl mailSender(){
}
@Bean
public SimpleMailMessage template(){
}
@Bean
public SimpleOrderManager orderManager(){
}
}
據我瞭解,你要使用標註來指定豆? (不是編程,不是在運行時)配置 http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/context/annotation/Configuration.html http://stackoverflow.com/questions/317687/how-can-i-inject-a-property-value-into-a-spring-bean-which-was-configured-using http://docs.spring.io/spring/docs/current/javadoc- api/org/springframework/beans/factory/annotation/Value.html – Vovka