<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="DaoImpl" class="Cuiyw.Spring.Dao.DaoImpl"></bean> <bean id="ServiceImpl" class="Cuiyw.Spring.Service.ServiceImpl" scope="singleton"> <property name="dao" ref="DaoImpl"></property> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="DaoImpl" class="Cuiyw.Spring.Dao.DaoImpl"></bean> <bean id="ServiceImpl" class="Cuiyw.Spring.Service.ServiceImpl" scope="singleton" name="ServiceA"> <property name="dao" ref="DaoImpl"></property> </bean> <alias name="ServiceA" alias="ServiceA1"/> </beans>
package Cuiyw.SpringAop;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import Cuiyw.Spring.IService.IService;
public class App
{
public static void main( String[] args )
{
ApplicationContext context=new ClassPathXmlApplicationContext(new String[]{"ApplicationContext.xml"});
BeanFactory factory=context;
IService service=(IService)factory.getBean("ServiceA1");
service.service("Cuiyw ServiceA1");
service=(IService)factory.getBean("ServiceA");
service.service("Cuiyw ServiceA");
service=(IService)factory.getBean("ServiceImpl");
service.service("Cuiyw ServiceImpl");
}
}
<property name="baseProperty" value="222"></property>
package Cuiyw.Spring.Service;
import Cuiyw.Spring.IDao.IDao;
import Cuiyw.Spring.IService.IService;
public class ServiceImpl implements IService{
private IDao dao;
private int baseProperty;
public IDao getDao() {
return dao;
}
public void setDao(IDao dao) {
this.dao = dao;
}
public void service(String name) {
System.out.println(dao.sayHello(name)+" baseProperty:"+getBaseProperty());
}
public int getBaseProperty() {
return baseProperty;
}
public void setBaseProperty(int baseProperty) {
this.baseProperty = baseProperty;
}
}
package Cuiyw.Spring.Dao;
import java.util.Calendar;
import Cuiyw.Spring.IDao.IDao;
public class DaoImpl implements IDao{
public String s;
public String getS() {
return s;
}
public void setS(String s) {
this.s = s;
}
public String sayHello(String name) {
int hour=Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
if(hour<6) return "凌晨早,"+name;
if(hour<12)return "早上好,"+name;
if(hour<13)return "中午好,"+name;
if(hour<18)return "下午好,"+name;
return "晚上好,"+name+", s="+s;
}
}
package Cuiyw.Spring.Service;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import Cuiyw.Spring.IDao.IDao;
import Cuiyw.Spring.IService.IService;
public class ServiceImpl implements IService{
private IDao dao;
private int baseProperty;
private List<Object> lists;
private Set<Object> sets;
private Map<Object, Object> maps;
private Properties pros;
public IDao getDao() {
return dao;
}
public void setDao(IDao dao) {
this.dao = dao;
}
public void service(String name) {
System.out.println(dao.sayHello(name)+",baseProperty:"+getBaseProperty());
for(int i=0;i<lists.size();i++)
{
Object obj=lists.get(i);
System.out.println(obj.getClass().getName());
}
for(Object obj : sets)
{
System.out.println(obj.getClass().getName());
}
//遍历maps中的key
for (Object key : maps.keySet()) {
System.out.println("Key = " + key);
}
//遍历maps中的值
for (Object value : maps.values()) {
System.out.println("Value = " + value);
}
Set<String> pro=pros.stringPropertyNames();
Iterator<String> it=pro.iterator();
while(it.hasNext()){
Object key=it.next();
System.out.println(key+"----"+pros.getProperty((String) key));
}
}
public int getBaseProperty() {
return baseProperty;
}
public void setBaseProperty(int baseProperty) {
this.baseProperty = baseProperty;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Set<Object> getSets() {
return sets;
}
public void setSets(Set<Object> sets) {
this.sets = sets;
}
public Map<Object, Object> getMaps() {
return maps;
}
public void setMaps(Map<Object, Object> maps) {
this.maps = maps;
}
public Properties getPros() {
return pros;
}
public void setPros(Properties pros) {
this.pros = pros;
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="DaoImpl" class="Cuiyw.Spring.Dao.DaoImpl">
<property name="s" value="cyw"></property>
</bean>
<bean id="ServiceImpl" class="Cuiyw.Spring.Service.ServiceImpl" scope="singleton" name="ServiceA">
<property name="dao" ref="DaoImpl"></property>
<property name="baseProperty" value="222"></property>
<property name="lists">
<list>
<value>1</value>
<ref bean="DaoImpl" />
<bean class="Cuiyw.Spring.Dao.DaoImpl">
<property name="s" value="cuiywlists" />
</bean>
</list>
</property>
<property name="sets">
<set>
<value>1</value>
<ref bean="DaoImpl" />
<bean class="Cuiyw.Spring.Dao.DaoImpl">
<property name="s" value="cuiywsets" />
</bean>
</set>
</property>
<property name="maps">
<map>
<entry key="key1" value="1"></entry>
<entry key="key2" value-ref="DaoImpl"></entry>
<entry key="key3" >
<bean class="Cuiyw.Spring.Dao.DaoImpl">
<property name="s" value="cuiywmaps" />
</bean>
</entry>
</map>
</property>
<property name="pros">
<props>
<prop key="prokey1">prokeyA</prop>
<prop key="prokey2">prokeyB</prop>
</props>
</property>
</bean>
<alias name="ServiceA" alias="ServiceA1"/>
</beans>
package Cuiyw.Spring.Service;
import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
public class CustomerProperty extends PropertyEditorSupport {
private String format="yyyy-MM-dd";
public String getFormat() {
return format;
}
public void setFormat(String format) {
this.format = format;
}
@Override
public void setAsText(String text) throws IllegalArgumentException {
// TODO Auto-generated method stub
SimpleDateFormat sdf=new SimpleDateFormat(format);
//super.setAsText(text);
try {
//转换对象,能过setValue方法重新赋值
this.setValue(sdf.parse(text));
} catch (ParseException e) {
e.printStackTrace();
}
}
}
<bean id="customEditorConfigurer" class="org.springframework.beans.factory.config.CustomEditorConfigurer"> <property name="customEditors"> <map> <entry key="java.util.Date" value="Cuiyw.Spring.Service.CustomerProperty"/> </map> </property> </bean>
<property name="date" value="2017-12-10"/>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有