public interface UserDao {
public void sayHello();
}
@Component(value="userDao")
public class UserDaoImpl implements UserDao {
@Override
public void sayHello() {
System.out.println("Hello Spring Annotation...");
}
Spring中提供@Component的三个衍生注解:(功能目前来讲是一致的)
* @Controller :WEB层
* @Service :业务层
* @Repository :持久层
这三个注解是为了让标注类本身的用途清晰,Spring在后续版本会对其增强
@Value :用于注入普通类型.
@Autowired :自动装配:
* 默认按类型进行装配.
* 按名称注入:
* @Qualifier:强制使用名称注入.
@Resource相当于:
* @Autowired和@Qualifier一起使用.
@Scope:
* singleton:单例
* prototype:多例
@PostConstruct :相当于init-method
@PreDestroy :相当于destroy-method
@PostConstruct说明
被@PostConstruct修饰的方法会在服务器加载Servlet的时候运行,并且只会被服务器调用一次,类似于Serclet的inti()方法。被@PostConstruct修饰的方法会在构造函数之后,init()方法之前运行。
@PreConstruct说明
被@PreConstruct修饰的方法会在服务器卸载Servlet的时候运行,并且只会被服务器调用一次,类似于Servlet的destroy()方法。被@PreConstruct修饰的方法会在destroy()方法之后运行,在Servlet被彻底卸载之前。
XML和注解:
* XML :结构清晰.
* 注解 :开发方便.(属性注入.)
实际开发中还有一种XML和注解整合开发:
* Bean有XML配置.但是使用的属性使用注解注入
关于注解1:工作中有一次犯了一个很严重的问题:
关于注解2:想要两个类使用同一个变量,而且两个类有关系,通过注入方式建立两个类的对象产生关系。但是如果想要共用一个对象,建立对象可以通过有参构造传入(new A("xxx")),但是注解建立对象我不会传参。但可以通过配置文件和注解相结合使用。
第二张图演示了properties文件属性的使用方法,在工作中,又遇见另外一种(在这里演示)
首先,在spring的主配文件中要配置:
<!-- 加载applicationConfig.properties文件,获取属性文件的内容 -->
<bean id="propertyConfigurer" class="com.ad.utils.GlobalProperties">
<property name="ignoreResourceNotFound" value="true" />
<property name="locations">
<list>
<value>classpath:applicationConfig.properties</value>
......
</list>
</property>
</bean>
然后写出实现类:
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
/**
* 自定义PropertyPlaceholderConfigurer返回properties内容
*
*/
public class GlobalProperties extends PropertyPlaceholderConfigurer{
private static Map<String, Object> ctxPropertiesMap;
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess,Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
/**
* 获取属性值
* @param key
* @return
*/
public static String getProperties(String key){
Object value = ctxPropertiesMap.get(key);
return value != null ? String.valueOf(value) : "";
}
/**
*获取属性值,返回×××
* @param key
* @return
*/
public static Integer getInteger(String key){
Object value = ctxPropertiesMap.get(key);
return value != null ? Integer.valueOf(value.toString()) : 0;
}
}
最后是使用方法:
GlobalProperties.getProperties("XXXXX").trim();
补:spring的bean管理xml方式
创建service类和dao类
在service中得到dao对象
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。