Meta Programming

treeexcellentΛογισμικό & κατασκευή λογ/κού

13 Δεκ 2013 (πριν από 3 χρόνια και 5 μήνες)

110 εμφανίσεις

Metaprogramming
 and    
Model-­‐View-­‐Controller  
with  Spring,  Hibernate,  and  Android  
2  
Typical  Spring  Applica=on  
POJI/POJO  
Spring  BeanFactory  
Bean  Wiring  
main()  or  Servlet  
3  
Mul=-­‐Arg  Constructor  Injec=on  


We  can  specify  the  order  of  constructor  arguments  
with  index  
<bean id="provider" class="com.simexusa.examples.Person">
<constructor-arg
index=

0

>
<value>David</value>
</constructor-arg>
<constructor-arg
index=

1

>
<value>Janzen</value>
</constructor-arg>
</bean>
public  class  Person  {  
       private  String  firstName,  lastName;  
       public  Person(String  fname,  String  lname)  {  
               this.firstName  =  fname;  
               this.lastName  =  lname;  
       }  
}  
4  
public  class  TestBooks  extends  TestCase  {    //in  examples/DIExamples/TestBooks.java  
 
public  void  testTitles()  {  
 
 
//write  testTitlesbeans.xml  
 
 
 
 
 
 
 
 
 
 
XmlBeanFactory  factory  =  new  XmlBeanFactory(new    
 
 
     
F i l e S y s t e m R e s o u r c e ("t e s t T i t l e s b e a n s.x m l") );  
 
 
B o o k s  b  =  ( B o o k s ) f a c t o r y.g e t B e a n ("s h e l f");  
 
 
a s s e r t E q u a l s ( b.g e t T i t l e s ( ).g e t ( 0 ),"S p r i n g  i n  A c = o n");  
 
}    }  
<bean id="shelf" class="Books">
<property name="
titles
">
<list>
<value>Spring in Action</value>
<value>Pro Spring</value>
<value>Hibernate Quickly</value>
</list>
</property>
</bean>
public  class  Books  {
 
//in  examples/DIExamples/Books.java  
 
private  
List
 =tles;  
 
public  void  
setTitles
(List  =tles)  {  this.=tles  =  =tles;  }  
 
public  List  getTitles()  {  return  this.=tles;  }  
}  
5  
Spring  MVC  Architecture  
Dispatcher  Servlet  
Controller  
View  
Model  
build.xml  
build.proper=es  
6  
Configuring  web.xml  


war/WEB-­‐INF/web.xml  
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC '-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN'
'http://java.sun.com/dtd/web-app_2_3.dtd'>
<web-app>
<
servlet
>
<servlet-name>
springapp
</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-
class>
<load-on-startup>1</load-on-startup>
</servlet>
<
servlet-mapping
>
<servlet-name>
springapp
</servlet-name>

<url-pattern>*.htm</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
Route all *.htm to this servlet
Identify a Spring DispatcherServlet
7  
Adding  a  Database  
Dispatcher  Servlet  
Controller  
View  
               Model  
build.xml  
build.proper=es  
web.xml  
springapp-­‐servlet.xml  
SpringappController.java  
hello.jsp  
hello.htm  
PriceIncreaseValidator.java  
ProductManager.java  
PriceIncreaseFormController.java  
priceincrease.jsp  
PriceIncrease.java  
priceincrease.htm  
messages.proper=es  
DB  
ProductManagerDaoJdbc.java  
8  
Configuring  Our  First  Mapping  


See  HibernateApplica=ons/ch03/src/java/com/simexusa/com  


Run  ch03/build.xml  then  look  in  HSQLDB  DatabaseManager  
package  com.simexusa.simple;  
public  class  One  {  
 
private  Long  id;  
 
private  int  something;  
 
public  void  setId(Long  id)  {  
 
 
this.id  =  id;  
 
}  
 
public  Long  getId()  {  
 
 
return  id;  
 
}    
 
public  void  setSomething(int  i)  {  
 
 
something  =  i;  
 
}  
 
public  int  getSomething()  {  
 
 
return  something;  
 
}  
}  
<?xml  version="1.0"?>  
<!DOCTYPE  hibernate-­‐mapping  PUBLIC  
       "-­‐//Hibernate/Hibernate  Mapping  DTD  3.0//EN"  
       "hmp://hibernate.sourceforge.net/hibernate-­‐
mapping-­‐3.0.dtd">  
 
<hibernate-­‐mapping  package="com.simexusa.simple">  
       <class  name="One"  table="one">  
               <id  name="id"  column="uid"  type="long">  
                       <generator  class="na=ve"/>  
               </id>  
               <property  name="something"  type="integer"/>  
       </class>  
</hibernate-­‐mapping>  
id
something
AndroidManifest.xml
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"

package=
"com.simexusa.adventdevotions"

android:versionCode=
"2"

android:versionName=
"1.0"
>
<application android:icon=
"@drawable/star"
android:label=
"@string/app_name"
android:debuggable=
"false"
>
<activity android:name=
".AdventDevos"

android:label=
"@string/app_name"
>
<intent-filter>
<action android:name=
"android.intent.action.MAIN"
/>
<category android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
<activity android:name=
".Devo"
/>
</application>
<uses-sdk android:minSdkVersion=
"3"
/>
<uses-permission android:name=
"android.permission.INTERNET"
/>
</manifest>
Specifies  icon  for  launching  app  
Specifies  name  for  launching  app  
Specifies  ac=vity  to  be  launched  at  startup  
Each  upload  to  Market  requires  versionCode  increment  
Security  permissions  requested  from  user  on  install  
layout.xml

<?xml version="1.0" encoding="utf-8"?>
<
LinearLayout

xmlns:android
="
http://schemas.android.com/apk/res/android"

xmlns:qwad
="
http://schemas.android.com/apk/res/com.simexusa.campusmaps_demo"

android:orientation
="vertical"

android:layout_width
="
fill_parent
"

android:layout_height
="
wrap_content
">

<
TextView


a n d r o i d:i d
="@ + i d/
m y L o c a t i o n
"

a n d r o i d:l a y o u t _ w i d t h
="
f i l l _ p a r e n t
"

a n d r o i d:l a y o u t _ h e i g h t
="
w r a p _ c o n t e n t
" />

<
R e l a t i v e L a y o u t


a n d r o i d:o r i e n t a t i o n
="v e r t i c a l"

a n d r o i d:l a y o u t _ w i d t h
="
f i l l _ p a r e n t
"

a n d r o i d:l a y o u t _ h e i g h t
="
f i l l _ p a r e n t
" >

<
c o m.s i m e x u s a.c a m p u s m a p s _ d e m o.C a m p u s M a p V i e w


a n d r o i d:i d
="@ + i d/m a p"

a n d r o i d:l a y o u t _ w i d t h
="
f i l l _ p a r e n t
"

a n d r o i d:l a y o u t _ h e i g h t
="
w r a p _ c o n t e n t
"

a n d r o i d:a d j u s t V i e w B o u n d s
="t r u e"

a n d r o i d:s c a l e T y p e
="
c e n t e r I n s i d e
"

a n d r o i d:s r c
="@
d r a w a b l e
/
c a m p u s m a p s i c o n
"

/>

<
Z o o m C o n t r o l s


a n d r o i d:i d
="@ + i d/
z o o m c o n t r o l s
"

a n d r o i d:l a y o u t _ a l i g n B o t t o m
="@ + i d/m a p"

a n d r o i d:l a y o u t _ a l i g n R i g h t
="@ + i d/m a p"

a n d r o i d:l a y o u t _ w i d t h
="
w r a p _ c o n t e n t
"

a n d r o i d:l a y o u t _ h e i g h t
="
w r a p _ c o n t e n t
" />

</
R e l a t i v e L a y o u t
>
</
L i n e a r L a y o u t
>